search for url in textarea as user types then trigger function
What i'm trying to do is search a textarea while the user is typing for a url beginning with http:// then if found, fire a function, and stop searching for an url.
I know this can be done with jQuery but I'm having quite a few issues with :contains, indexOf, regex, etc.
heres a bit of code to explain what I'm trying to do!
$(document).ready(function() {
var body = $('#example').attr('value');
body = encodeURIComponent(body);
var urlregex = new RegExp("^(http:\/\/www.|http:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
if(urlregex.test(body)) {
$("#new_url").html('../images/loading.gif" border="0">');
$.ajax({
type: "GET",
url: "get_url.php",
data: "entry="+body,
cache: false,
success: 开发者_C百科function(html) {
$("#new_url").html(html);
}
});
return false;
}
return(false);
}
});
Thanks for all your help...
You code looks a little messy. Is this what you are after?
var myFunction = function() {
console.log('URL found.');
}
$('textarea').bind('keyup', function(e) {
if ($(this).val().match(/http:\/\//)) {
myFunction.call(this);
$(this).unbind('keyup', arguments.callee);
}
});
Below function gets all the urls found in textarea
var myFunction = function() {
console.log('URL found.');
}
$("textarea").keypress(function (e) {
if ((e.which && e.which == 32) || (e.keyCode && e.keyCode == 32)) {
if ($(this).val().match(/https?:\/\//)) {
myFunction.call(this);
}
}
});
精彩评论