window.open behaviour in chrome tabs/windows

后端 未结 2 1257
野性不改
野性不改 2020-12-02 00:01

I have a small bit of javascript intended to open two or more tabs. This works fine in FF and IE, but chrome opens the second one in a new window instead of tab. It isn\'t d

2条回答
  •  难免孤独
    2020-12-02 00:45

    I came across this question What is the (function() { } )() construct in JavaScript? which gives explanation on IIFE. I think this can be used over. Please bear with me I don't have deep knowledge about javascript. But I tried as below and its working.

    var sites = [{"url" : "http://www.google.com"} , {"url" : "http://www.yahoo.com"} , {"url" : "http://www.msn.com"}];
    console.log(sites);
    for( var i=0 ; i < sites.length ;i++) {
        (function(i) {
            console.log(i);
            window.open(sites[i].url , "_blank");
        })(i);
    }   
    

    It opens the url in new tabs in chrome.

提交回复
热议问题