javascript - this.platform.is("mobile") returning true for browser - Stack Overflow

I am running a ionic project on browserscript"browser": "ionic-app-scripts serve --sourc

I am running a ionic project on browser

script

"browser": "ionic-app-scripts serve --sourceMap source-map --iscordovaserve --wwwDir platforms/browser/www/ --buildDir platforms/browser/www/build"

and running npm run browser

I am trying this piece of code in appponent.ts

if (this.platform.is('ios') || this.platform.is('android') || this.platform.is('mobile'))
//execute certain function only on mobile

Problem

this.platform.is('mobile') and this.platform.is('android') is returning true to me in case of browser which is weird behaviour.

So , how to obtain the condition so that on browser i want to disable certain feature and why is the above code not working as expected. Thanks.

I am running a ionic project on browser

script

"browser": "ionic-app-scripts serve --sourceMap source-map --iscordovaserve --wwwDir platforms/browser/www/ --buildDir platforms/browser/www/build"

and running npm run browser

I am trying this piece of code in app.ponent.ts

if (this.platform.is('ios') || this.platform.is('android') || this.platform.is('mobile'))
//execute certain function only on mobile

Problem

this.platform.is('mobile') and this.platform.is('android') is returning true to me in case of browser which is weird behaviour.

So , how to obtain the condition so that on browser i want to disable certain feature and why is the above code not working as expected. Thanks.

Share Improve this question asked Aug 22, 2018 at 18:44 manish kumarmanish kumar 4,7104 gold badges39 silver badges52 bronze badges 2
  • Is this app gonna be used in the browser too? Or just in the mobile devices? – Sujan Gainju Commented Aug 22, 2018 at 18:59
  • Just the mobile devices. But it throws error when we test in the browser.So just want to skip that on browser while development. – manish kumar Commented Aug 22, 2018 at 19:02
Add a ment  | 

5 Answers 5

Reset to default 4

There are two approaches i got.

As suggested By Sergey

let isWebApp = this.platform.url().startsWith('http');

Found on the Ionic Forum

!document.URL.startsWith('http');

These are mainly used for development as development process is faster is in browser as changes are reflected instantly.

I really don't think you need this but you can use:

 if(this.platform.is('core') || this.platform.is('mobileweb')) {
        // In Browser
      } else {
        // In Mobile
      }

You can use other way to detect whether your app is web app or a cordova hybrid app:

let isWebApp = this.platform.url().startsWith('http');

In cordova (on device basically) depending on platform the app's "url" will start from "file://" or other but not http.

If you Enabled Toggle device toolbar from browser, It'll return mobile. and if it is disabled then return desktop.

You can see it'll return desktop by disabling Toggle device toolbar from the browser:

You have to refresh(f5) the page after changing the state of Toggle device toolbar or it'll not work, because it was cached.

You can try this condition for mobile app only

  !isPlatform('mobileweb') && isPlatform('mobile')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745049092a4608274.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信