如何识别网页是否在 PWA 环境下运行?从而判断是否展示内容

想要判断是不是在PWA中打开的网站,该怎么做?终于在Stack Overflow找到了答案

有能力的可以看原文:progressive web apps - Javascript to check if PWA or Mobile Web - Stack Overflow

This works for all - TWA, Chrome & Safari:下面的方法可以成功运行

const isInStandaloneMode = () =>
      (window.matchMedia('(display-mode: standalone)').matches) || (window.navigator.standalone) || document.referrer.includes('android-app://');

 if (isInStandaloneMode()) {
    console.log("webapp is installed")
}

我自己使用js做了一个判断:

    const isInStandaloneMode = () =>
        (window.matchMedia('(display-mode: standalone)').matches) || (window.navigator.standalone) || document.referrer.includes('android-app://');

    if (isInStandaloneMode()) {
        document.body.innerHTML = "是在pwa中打开的"
    }else {
        document.body.innerHTML = "请在pwa中打开"
    }

 当不是在pwa中打开的话:网页内容展示

如果是在pwa中打开的话:

猜你喜欢

转载自blog.csdn.net/weixin_44786530/article/details/130523638
PWA