简体   繁体   English

自动加载一页上的所有链接

[英]Auto load all link on one pages

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 JavaScript? JavaScript有可能吗? 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. 如果element是link,则执行所需的操作。 You can use AJAX. 您可以使用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: 使用这样的纯JavaScript代码可能:

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/ 实时测试案例: http//jsfiddle.net/w5tdv/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM