admin管理员组文章数量:1387325
We have salesforce lightning app which runs on both mobile and desktop. Need to write some code only for mobile application. How to detect whether app is running on a mobile browser or desktop browser? I used following code but its not working:
checkMobileBrowser: function(ponent){
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
return true;
}else{
return false;
}
}
We have salesforce lightning app which runs on both mobile and desktop. Need to write some code only for mobile application. How to detect whether app is running on a mobile browser or desktop browser? I used following code but its not working:
checkMobileBrowser: function(ponent){
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
return true;
}else{
return false;
}
}
Share
Improve this question
edited Jun 20, 2016 at 8:05
Hleb
7,39115 gold badges64 silver badges127 bronze badges
asked Jun 16, 2016 at 10:40
KittyKitty
1676 silver badges14 bronze badges
3 Answers
Reset to default 3The $Browser global value provider returns information about the hardware and operating system of the browser accessing the application.
Returns a FormFactor enum value based on the type of hardware the browser is running on.
DESKTOP for a desktop client PHONE for a phone including a mobile phone with a browser and a smartphone TABLET for a tablet client (for which isTablet returns true)
Ctrl
({
checkBrowser: function(ponent) {
var device = $A.get("$Browser.formFactor");
alert("You are using a " + device);
}
})
Component
<aura:ponent>
{!$Browser.isTablet}
{!$Browser.isPhone}
{!$Browser.isAndroid}
{!$Browser.formFactor}
</aura:ponent>
By using this way you can determine
Resource: http://www.janbask./salesforce-lightning
You can also use the $Browser
global value provider:
function checkMobileBrowser(){
return $A.get("$Browser.formFactor") !== "DESKTOP"
}
This will ensure that your detection matches what the application uses, if your ponent is ever embedded in S1 or SFX, and everything will switch on the same logic.
$Browser
is also available in ponent markup:
<aura:if "{!$Browser.formFactor !== 'DESKTOP'}">
<ponent/>
</aura:if>
You might want to search the documentation for $Browser
, because it allows for very granular hardware detection, and there might be something else for your specific use case.
https://resources.docs.salesforce./sfdc/pdf/lightning.pdf
you can write JavaScript function for validation of browser's user agent:
function detectmob(){
if(navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)){
return true;
}else{
return false;
}
}
本文标签:
版权声明:本文标题:javascript - How to detect whether salesforce lightning app is running on a mobile browser or desktop browser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744570256a2613295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论