【问题标题】:Window.navigator on other browsers in mac os not working?mac os 中其他浏览器上的 Window.navigator 不起作用?
【发布时间】:2022-01-02 02:27:47
【问题描述】:

我正在尝试访问我的项目中的导航器功能以共享所需的 URL,但是当它是 Safari 以外的其他浏览器时,我无法在 Mac OS 中访问它。有没有办法扭转这个问题?

这是我为分享函数定义的函数:

    function shareFunction() {
        if(navigator.share) {
            navigator.share({
                title: document.title,
                url: window.location.href
            })
            .then(() =>{
                setShareClick(true)
            })
            .catch(err => console.error(err));
        }
    }

这适用于 Mac OS 中的 Safari 和所有其他 Windows 浏览器,但不适用于 Mac Chrome 或 Firefox,我如何在 mac chrome 中生成共享表?

【问题讨论】:

标签: javascript next.js navigator


【解决方案1】:

看起来这个功能并没有在所有浏览器中实现,也许是一个新功能?查看https://caniuse.com/?search=navigator.share 了解哪些浏览器支持此功能。

作为替代方案,您可以通过这种方式显示自定义共享弹出窗口:

function shareFunction() {
    if (navigator.share) {
        // Your normal native share code here
    } else {
        // Toggle a custom popup here
    }
}

【讨论】:

  • 查看规范草案,您的情况最终会正确地类似于:if (navigator.share && navigator.canShare && navigator.canShare(data)) { navigator.share(data) } else { ... }w3c.github.io/web-share/#canshare-data-method
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多