Reputation: 3382
I am trying to get the value of a text input and check if there is any links in it and then take those links and make them into tags. But when I run this code, something is going wrong and it completely freezes the page. Basically, I want it to check for "http://" and if that exists, to keep on adding to the substr length until the end of the string/link. Is there a better way to do this?
// the id "post" could possibly say: "Hey, check this out! http://facebook.com"
// I'd like it to just get that link and that's all I need help with, just to get the
// value of that entire string/link.
var x = document.getElementById("post");
var m = x.value.indexOf("http://");
var a = 0;
var q = m;
if (m != -1) {
while (x.value.substr(q, 1) != " ") {
var h = x.value.substr(m, a);
q++;
}
}
Upvotes: 0
Views: 94
Reputation: 8368
Of course it is – there is an infinite loop.
You probably wanted to update the variable q
in each iteration.
q = q + a;
or just q += a;
Update:
I see you changed the code a bit.
I get what you're trying to do. You're trying to get a URL from an input value.
Why don't you just use a simple RegExp
instead of this unclear loop?
var match = x.value.match(/(?:^|\s)(http:\/\/\S+)/i);
var url = match ? match[1] : null;
Upvotes: 3