Javascript to check if PWA or Mobile Web

前端 未结 5 1814
野的像风
野的像风 2020-12-12 12:12

I was curious if anyone knew a javascript based method for detecting whether the web experience was being run as a PWA (progressive web app) or it was simply being run as a

5条回答
  •  醉酒成梦
    2020-12-12 12:31

    if (window.matchMedia('(display-mode: standalone)').matches) {
      console.log("This is running as standalone.");
    }
    

    This answer is correct but it's worth to mention that PWA could run on plenty of display modes:

    • fullscreen
    • standalone
    • minimal-ui
    • browser

    If you run your PWA in a 'fullscreen' mode it will return false so additional checks are necessary like:

    function isPwa() {
        return ["fullscreen", "standalone", "minimal-ui"].some(
            (displayMode) => window.matchMedia('(display-mode: ' + displayMode + ')').matches
        );
    }
    

    Note that window.matchMedia check will return true for the 'browser' display mode even when it's not an installed PWA app.

提交回复
热议问题