window.open('','_blank')仅在iOS上不会打开新标签页

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

我通过_blank调用window.open,它在除iOS之外的所有浏览器中均可使用。在我的Web应用程序中,当我单击iOS设备上的“添加到购物车”按钮时,什么都没有发生,而在所有其他浏览器中,将打开一个新窗口。

const addProducts = (products) => {
    setProductsAdded(false);
    cService
      .add(products)
      .then(() => {
        setLoading(null);
        setProductsAdded(true);
        window.open(C_LINK, '_blank');
      })
      .catch(() => {
        setError('Error');
      });
  };

我发现了这个问题和答案,这似乎是相同的问题-但是我是Java语言的新手,不确定如何实现它:window.open(url, '_blank'); not working on iMac/Safari

所以,我的第一个问题是,我刚才提到的问题和答案是否可能是同一个问题?我的第二个问题是,如果我要尝试实现上一个问题中提到的解决方案,我会修改现有功能还是将其分开?我在哪里设置window.open()?有人可以解释“ myService”到底是什么吗?谢谢您的帮助。

javascript ios mobile-safari
2个回答
0
投票

我以前没有尝试过,但是从SO答案中实现解决方案看起来像这样:

// Open a new window and save a reference to it
var newWindow = window.open();

const addProducts = (products) => {
    setProductsAdded(false);
    cService
      .add(products)
      .then(() => {
        setLoading(null);
        setProductsAdded(true);

        // Set the URL of the new window
        newWindow.location = C_LINK;
      })
      .catch(() => {
        setError('Error');
      });
  };

在另一篇文章中,myService是不存在的示例服务,它返回URL。


0
投票

为此,您需要将URL绑定到元素onclick事件。

请遵循代码。在HTML中添加此按钮。

<button class='btn' id="btnClick" onclick='window.open("https://www.google.com", "_blank");' style='position: absolute;top: 0;display: none'>Open Google search</button>

在onclick事件中替换您的链接。现在,使用javascript触发click事件。

更改window.open(C_LINK, '_blank');

document.getElementById("btnClick").click()

REFERENCE

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