在<a> tag open multiple times</a>中建立链接

时间:2014-04-08 07:19:20

标签: javascript html

我想知道如何在html中创建一个链接,打开耦合的URL 10次或更多次。

但是,我没有恶意。我只想恶作剧。

1 个答案:

答案 0 :(得分:1)

使用HTML,您只能打开一个包含a标记的链接;请看一下JavaScript,window.open()quoting from MDN doc):

  

var windowObjectReference = window.open(strUrl,strWindowName [,strWindowFeatures]);

示例:

window.open('http://www.example.com'); //You maybe don't need to keep a 
                                      //reference of the newly opened window here

在需要的时候多打电话。

请注意,它会像弹出窗口一样,可能会被浏览器自动阻止(例如Chrome 33)。

在行动中

您可以拥有以下链接:

<a href="http://www.goodjoke.com" id="prank_link">

在JS中(包含在您的html文件中,headbody的末尾):

<script>
    //When the document is "loaded", execute the following code
    document.onload = function(){
        //Get the a element, identified by its ID
        var prank_link = document.querySelector('#prank_link'); 

        prank_link.onclick = function(e){
            e.preventDefault();            //Prevent the page to be changed
            var my_url = this.href;        //Store the url in the link

            for(i = 0; i < 10; i++){       //Call window.open() 10 times with your URL
                window.open(my_url);    
            }
        }
    }
</script>

这是live demo