开发者

javascript looping problem

开发者 https://www.devze.com 2023-02-08 07:39 出处:网络
<script type=\"text/javascript\"> function addbutton() { var o = document.getElementsByTagName(\'li\');
<script type="text/javascript">
    function addbutton() {

    var o = document.getElementsByTagName('li');
    for(var i=0;i<o.length;i++) 
    {
     var keyword= o[i].innerHTML;
     var regobj = /<a (.*)>(.*)<(.)a>/g;
    开发者_JAVA百科 keyword = keyword.replace(new RegExp("<br>", "g")," ");
     keyword = keyword.replace(regobj,"$2");
     keyword = keyword.substring(0,100);
     var str = "<br>"+ "<a class='but' target='blank' ";  
     str += "href=\"http://www.google.com.tr/search?q=";
     str += decodeURIComponent(keyword);
     str += "\">ara</a>";
     o[i].innerHTML = o[i].innerHTML + str;

    }
}
</script> 

addbutton() adds a search button to end of the li tags, but when i run, it's looping for only 43 times. so it's adding the search button for the first 43 li tags. what is your thoughts, why is looping broken?

edit: more information,

-i tried function on a page that contains 131 li tags. and function is working for the first 43 li tag,

-i tried for both of google chrome and firefox, results are same.

i tried to alert(o.length). there is no problem, it return true number, "131".


Well, there's certainly a bug on this line:

str += decodeURIComponent(keyword);

That should be

str += encodeURIComponent(keyword);

That may be the problem, decodeURIComponent may be blowing up on some input text because you're using it on strings that aren't URL-encoded.

Barring that:

You'll need to look at the actual text in the li elements as they're being processed. It's clearly not just a numbers thing, the code works just fine in Chrome.

I recommend using Chrome's Dev tools (Shift+Ctrl+I) to set a breakpoint in the function and walk through it with your actual test data. Or use the Firebug add-on for Firefox to do the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消