开发者

Auto load all link on one pages

开发者 https://www.devze.com 2023-03-23 21:32 出处:网络
I want to make a list of links in a page and when we open the page, it will automatically open/load the links in sequence by a certain time delay.

I want to make a list of links in a page and when we open the page, it will automatically open/load the links in sequence by a certain time delay.

Is this possible with JavaScri开发者_JAVA技巧pt? I'm not so knowledgeable, I hope someone help me thanks.


Yes, you can do this. You have to iterate over all elements of the document. If element is link, do what you want. You can use AJAX.

But I do not really understand how will you "load the link". What will you do with it? Store in memory? But what will happen when user clicks the link?


Possible, with such pure JavaScript code:

window.onload = function() {
    var anchors = document.getElementsByTagName("a");
    var links = [];
    for (var i = 0; i < anchors.length; i++) {
        curHref = anchors[i].href;
        if (curHref.length > 0)
            links.push(curHref);
    }
    OpenLink(links, 0);
};

function OpenLink(links, index) {
    if (index >= links.length)
        return false;
    window.open(links[index], "_blank");
    window.setTimeout(function() {
        OpenLink(links, index + 1);
    }, 1000);
}

This will iterate over all the links and open each as pop up window - modern browsers will block this by default so user will have to enable pop up windows for your website.

If you mean something else, please edit your question and clarify.

Live test case: http://jsfiddle.net/w5tdv/

0

精彩评论

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