繁体   English   中英

javascript window.open 从回调

[英]javascript window.open from callback

默认情况下,从主线程调用的window.open()打开新选项卡。

但是,这里每次都打开新窗口(Opera 16 和 Google Chrome 29)

<input type="button" value="Open" onclick="cb1()">

<script type="text/javascript">  
function cb1() {
    setTimeout(wo, 1000); //simple async
}

function wo()
{
   var a = window.open("http://google.com", "w2");
   a.focus();
}
</script>

(大声笑,这是我使用 JavaScript 在新选项卡(而不是新窗口)中打开 URL 的答案)。

我如何在选项卡中打开(默认浏览器)?

我们遇到了同样的问题并四处寻找答案。 我们发现在我们的情况下有效的方法和提炼的智慧如下:

该问题与阻止程序化窗口打开的浏览器弹出窗口阻止程序有关。 浏览器允许从主线程上发生的实际用户点击打开窗口。 同样,如果您在主线程上调用 window.open 它将起作用,如上所述。 根据使用 JavaScript 在新选项卡(而不是新窗口)中打开 URL上的这个答案,如果您使用 Ajax 调用并希望成功打开窗口,您需要设置async: false ,因为这将保留所有内容在主线程上。

我们无法像那样控制我们的 Ajax 调用,但由于相同的原因找到了另一个有效的解决方案。 警告,它有点老套,鉴于您的限制,它可能不适合您。 感谢对使用 JavaScript 在新选项卡(而不是新窗口)中打开 URL的不同答案的评论,您在调用setTimeout之前打开窗口,然后在延迟函数中更新它。 有几种方法可以做到这一点。 打开窗口时保留对窗口的引用, w = window.open...并设置w.location或使用目标打开, window.open('', 'target_name') ,在延迟函数中打开target, window.open('your-url', 'target_name') ,并依赖浏览器保持引用。

当然,如果用户有在新窗口中打开链接的设置,这不会改变这一点,但这对 OP 来说不是问题。

就像其他帖子提到的那样,最好的方法是先打开窗口,然后在回调或异步函数之后设置其位置

<input type="button" value="Open" onclick="cb1()">

<script type="text/javascript">

function cb1() {
  var w = window.open('', 'w2');
  setTimeout(function () {
    wo(w);
  }, 1000); //simple async
}

function wo(w)
{
  w.location = "http://google.com";
  w.focus();
}
</script>

或者,如果您使用 async await,您也会遇到同样的问题。 相同的解决方案仍然适用。

public async openWindow(): Promise<void> {
    const w = window.open('', '_blank');
    const url = await getUrlAsync();
    w.location = url;    
}

进一步的增强是在初始页面上打开窗口,通过加载 url 或向该页面写入一些 html 来提供一些快速反馈

public async openWindow(): Promise<void> {
    const w = window.open('', '_blank');
    w.document.write("<html><head></head><body>Please wait while we redirect you</body></html>");
    w.document.close();
    const url = await getUrlAsync();
    w.location = url;    
}

这将防止用户查看空白选项卡/窗口花费的时间来解析您的 URL。

如果将新窗口作为新选项卡或新实例打开,则取决于用户设置。

我正在使用NestjsVue3 但我使用这里写的代码解决了它。 我只是在localhost:8080上获取了从后端发送的令牌。 所以我只是将令牌切片并将其设置为本地存储,然后将用户重定向到下一页。 这将授权用户使用此令牌 3 . 这样就解决了。 你可以改进它,让它变得更好。 我就是这样做的,因为我在后端使用了OAuth google-passport而不是 firebase

<script>

methods: {
     googleLogin() {
      const win = window.open(
        "http://localhost:3000",
        "windowname1",
        "width=800, height=600"
      );
      const validateToken = (token) => {
        localStorage.setItem("token", token);
        this.$router.push("/GeneralPage");
        window.clearInterval(pollTimer);
        clearInterval(pollTimer);
        win.close();
      };
      const pollTimer = window.setInterval(async () => {
        try {
          let url = win.document.URL;
          let token = url.slice(22, url.length);
          if (token !== "") {
            localStorage.setItem("token", token);
            this.$router.push("/GeneralPage");
            clearInterval(pollTimer);
            win.close();
            validateToken(token);
            return;
          }
          return;
        } catch (e) {}
      }, 1000);
    },
}
</script>

这甚至是“黑客”,但是......

如果您将 window.open 包装在 console.log 中,那么它将起作用。

console.log(window.open('https://someurl', '_blank'))

暂无
暂无

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

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