我正在尝试获取文本输入的值,并检查其中是否有链接,然后将这些链接添加到标记中。但是当我运行这个代码时,出现了一些问题,它完全冻结了页面。基本上,我希望它检查“http://”,如果存在,继续添加到substr长度直到字符串/链接结束。有一个更好的方法吗?运行此代码时Javascript正在冻结浏览器
// 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++;
}
}
这是冻结,因为你几乎肯定有潜力无限循环。 –
我试图修复它D: – Zachrip
另外,你的变量有可怕的名字。代码应该能够被看作和粗略理解。这里不是这种情况。 –