如何在 safari iPhone 上的新窗口中打开网址

问题描述 投票:0回答:6

每当我尝试使用

window.open()
打开 URL 时,都不会打开任何窗口。所以我尝试了这个

<input
  id="googleLink"
  onclick="window.open('https://www.google.com', '_blank');"
  style="display: none"
>

我尝试用 Typescript 单击它

this.googleLink = document.getElementById("googleLink") as HTMLElement;
this.googleLink.click();

并阅读本文

window.open(url, '_blank');无法在 iMac/Safari 上工作

但这些都没有帮助。我没有在异步函数中运行我的代码。我能做些什么?我有 iPhone 11 和 7,两者均无法使用。它在安卓和电脑上运行得很好。我需要在新窗口中打开该 URL。

我需要使用

onClick
来执行此操作,因为我使用的是 pixi js 并且它是一个被单击的 pixi 容器。

设置 href 也无济于事,因为我希望页面在新选项卡中打开。

html typescript window.open
6个回答
3
投票

这也应该适用于 Safari,但您可以通过添加

target="_blank"
向链接添加目标。这会在新选项卡中打开链接,并且在 Safari 上也应该在新窗口中打开。这是一个例子:

<a href="https://example.com" target="_blank">Link text.</a>

在您的情况下,将输入包装在链接中:

 <input
  id="googleLink"
  style="display: none"
>

如果您不想使用 html 而是使用 onclick 函数来执行此操作,您也可以使用

onclick="location.href='https://example.com'"


1
投票

您可以尝试以下方法

    <canvas id="myCanvas" width="200" height="100" style="border: 1px solid #000;" onclick="myFunction('thisURL')"></canvas>


  <script>
    function myFunction(url) {

      window.open(url, '_blank').focus();

    }
  </script>

1
投票

您是否检查过 Safari 安全设置中是否禁用了

Block pop-up windows
?同样的事情也适用于移动版本的 Safari。


1
投票

我做了类似的事情:

  const aElem = document.createElement("a");
  aElem.href = url;
  aElem.target = "_blank";
  document.body.append(aElem);
  aElem.click();
  aElem.remove();

就像魔术一样:)


0
投票

唯一适合我的方法。

let wnd = window.open('about:blank', '_blank');
if(!wnd){alert("Browser block window open");return;}
wnd.location.href = url;

-1
投票

尝试在脚本中用 defer 属性替换 async。

© www.soinside.com 2019 - 2024. All rights reserved.