How to detect if I am in browser (local development) in Ionic 2

前端 未结 7 1242
太阳男子
太阳男子 2020-12-03 03:00

I want to make sure to launch inappbrowser only if I am on devices (iOs, Android...), but if I am in browser (local development mode or just a Web App with gulp build), I wa

7条回答
  •  旧时难觅i
    2020-12-03 03:14

    We have a simple way to know if we are running the '/www' folder of the project in browser, or in a device (emulate/build).

    In local projects of Cordova/Phonegap, the index.html has this label:

    
    

    But this file does not exists, because Cordova/Phonegap will inject phonegap.js or cordova.js when we emulate or build. And if we run www folder in browser, always can see an error like this:

    GET file:///D:/Cordova/Workspace/TestProject/www/cordova.js net::ERR_FILE_NOT_FOUND
    

    Then we can check in the index.html label if this file loads or not. If loads, we are in App, if not, we are in browser (local):

    
    

    We think is the fastest way to know where are we.

提交回复
热议问题