【问题标题】:window.open not not able to open more than two linkswindow.open 无法打开两个以上的链接
【发布时间】:2012-11-08 04:59:34
【问题描述】:

根据我的要求,我需要创建一个 Google Chrome 扩展程序,在单个 chrome 窗口的不同选项卡中单击即可打开多个链接 (25+)。该代码在 Chrome 18 之前运行良好。现在,我使用的是 chrome 24,该代码停止工作。我只是将所有链接存储在一个数组中,然后使用 for 循环打开它们,如下所示:

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

因此,只有两个链接在选项卡中打开,其余链接将在不同的 chrome 窗口中打开。我应该怎么做才能克服这个问题?

编辑#1

在我的清单文件中

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


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

首先给出的代码在dialog.js中

【问题讨论】:

  • 如果这真的是一个扩展,你为什么不使用tab api
  • I need to open multiple links (25+) on a single click 25 个标签同时打开... o.O
  • 我尝试过使用 chrome.tabs.create({"url":tablink});但它不起作用
  • 我试过这个 window.open("google.co.in/");window.open("https://www.facebook.com/…; window.open("yahoo.com/"); window.open("in.com/"); 但它没有在新窗口上打开?
  • @SaurabhSaxena 你能分享 chrome.tabs.create({"url":tablink}); 的代码吗,它对我有用吗?

标签: javascript jquery html google-chrome google-chrome-extension


【解决方案1】:

这似乎是 JavaScript 中的常见错误。 setTimeout(window.open(tablink), 500); 表示在 500 毫秒后调用 window.open 返回的内容。 window.open 的返回值通常是一个 Window 对象,这会使 setTimeout 失败并且您的代码停止执行。这就是导致问题的原因。 请改用setTimeout(function(){window.open(tablink)}, 500);

【讨论】:

    【解决方案2】:

    我尝试同时打开多个网站,

    发现“该页面上的弹出窗口被阻止”

    你可以在地址栏中看到。

    :)

    【讨论】:

    • 检查地址栏并单击始终允许弹出窗口单选按钮
    【解决方案3】:

    找到解决方案,打 n 块试石 :)

    我刚刚删除了 setTimeout 函数,它可以工作。我仍然不明白为什么它会导致问题。

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

    【讨论】:

      猜你喜欢
      • 2021-08-13
      • 2022-01-27
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多