views:

88

answers:

1

Hi,

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: function(html) {
                    $("#new_url").html(html);
                }
            });
            return false;
  }
  return(false);
}
});

Thanks for all your help...

+2  A: 

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);
    }
});
David
+1. Very slick.
czarchaic
That worked awesome. I made a mistake in my question... Instead of looking for http:// how can i setup the .match for finding a url after the user is done entering the url. IE: after they type the .com, .net, .org, etc. Thanks so much... I got alot to learn about jQuery
brandon
@Brandon: Just replace the regexp to match entire URLs.
David
thanks alot david!
brandon
Hey david... that works awesome... check out the test page - type a few words and then type a url start with http:// then wait a few seconds... and try to keep typing... it still tries to search... http://top10ingoogle.com/js/test.html
brandon
@Brandon: You are returning false before unbinding the callee.
David
Dude! You are awesome! You have no idea how long I've been working on this little demo. It works perfect now. Thank you so much. Happy New Year!
brandon
Hey david thanks for the input... I went a different route! If i bind /unbind the function if the user changes the url or backspaces it doesn't search anymore... check out what I did... much more effective than facebooks url fetcher thingy for posts! Agree?
brandon