window.open not not able to open more than two links

≡放荡痞女 提交于 2020-01-03 18:00:31

问题


As per my requirement, i need to create a Google Chrome Extension which opens multiple links (25+) on a single click in different tabs of a single chrome window. The code was working fine earlier till the Chrome 18. Now, I am using chrome 24 and that code stopped working. I was simply storing all the links in an array and opening them using a for loop as follows:

  for(var i = 0; i<links.length; i++)
  {
    var tablink = links[i];
    if(links[i] != "")
    {
            tablink = *"somedomain"* + tablink;
        setTimeout(window.open(tablink), 500);  
    }
  }  

As a result, only two links were open in tabs and rest will open in different chrome windows. What should i do to overcome this?

Edit #1

In my manifest file

"content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["script.js", "jquery.js", "dialog.js"]
    }
  ],


"permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],

The code given first is in dialog.js


回答1:


This seems to be a common error in JavaScript. setTimeout(window.open(tablink), 500); means to invoke what window.open returns after 500 milliseconds. The return value of window.open is typically a Window object, which makes setTimeout fail and your code stops executing. That's what caused the problem. Please use setTimout(function(){window.open(tablink)}, 500); instead.




回答2:


Got the solution, hit n trial rocks :)

I just removed setTimeout function and it works. I still don't why it was causing the problem.

for(var i = 0; i<links.length; i++)
  {
    var tablink = links[i];
    if(links[i] != "")
    {
            tablink = *"somedomain"* + tablink;
        window.open(tablink);  
    }
  }  



回答3:


I tried to open number of sites at the same time,

found that "pop-ups were blocked on that page "

you can see that in address-bar.

:)



来源:https://stackoverflow.com/questions/13467915/window-open-not-not-able-to-open-more-than-two-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!