开发者

Best way to run working javascript onload after delay

I am trying to make a greasemonkey script that will run the following code. I have tested it and it definitely works via URL (javascript: blah blah;) minus the comment of course:

// Your country code
var cc = 'net';

var j = document.getElementsByTagName('td');
for (i=0; i <= j.length; i++) {
    if (j[[i]].innerHTML.indexOf('points - punishment #A') > 0 && j[[i]].innerHTML.length == 30) {
        var k = j[[i]].innerHTML;
        j[[i]].innerHTML = k.slice(0, 23) + '<a href="http://www.example.' + cc + '/admin.php?screen=affront_tool&mode=view_punishment&punishment_id=' + k.slice(25) + '">' + k.slice(23) + '</a>';
    }
    else if (j[[i]].innerHTML.indexOf('points - punishment #A') > 0 && j[[i]].innerHTML.length == 29) {
        var k = j[[i]].innerHTML;
        j[[i]].innerHTML = k.slice(0, 22) + '<a href="http://www.example.' +开发者_Python百科 cc + '/admin.php?screen=affront_tool&mode=view_punishment&punishment_id=' + k.slice(24) + '">' + k.slice(22) + '</a>';
    }
    else if (j[[i]].innerHTML.indexOf('points - punishment') > 0 && j[[i]].innerHTML.length <= 67) {
        var k = j[[i]].getElementsByTagName('a')[0];
        var l = 'http://www.example.' + cc + '/admin.php?screen=affront_tool&mode=view_punishment&punishment_id=' + k.getAttribute('href').slice(19);
        k.setAttribute('href', l);
    }
}

Unfortunately this script works on an element of the page dynamically generated up to ten seconds after load (expect about three), which makes things difficult. I've tried numerous ways of attaching the script as an "onload" attribute to the body with a setTimeout of ten thousand ms, to no avail. Perhaps I should be trying a different method, or I've repeated a simple mistake in the process? Could someone show me how they would do it?


I would include the jquery library on your page and make your code into a function then do:

$(document).ready(function () {
           setTimeout('myFunction();', 10000);
        });

In this case the function name is "myFunction()"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜