Created
May 2, 2012 13:20
-
-
Save vasilisvg/2576485 to your computer and use it in GitHub Desktop.
Check if the page is viewed in the Safari browser in iOs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
I want to make a bubble that explains people that they can save the page to their homescreen | |
I want to be absolutely sure that this is Safari on an iPhone or iPad | |
Not a browser in a webview in a native app | |
Not a browser in standalone mode | |
Not something else | |
This is the closest I can get to something I kinda trust. | |
*/ | |
var nav = window.navigator; | |
var ua = nav.userAgent; | |
function isiOsSafari (a) { | |
return ("standalone" in nav) // There's a thing called standalone in nav | |
&& !nav.standalone // It is not running in standalone mode | |
&& ua.indexOf(a)!=-1 // iPhone is in the UA string (could be Opera) | |
&& ua.indexOf('Mac OS')!=-1 // There's Mac in the UA string (not Opera) | |
&& ua.indexOf('Safari')!=-1 | |
/* if all the above are true this probably means this is | |
the Safari browser, | |
not a webview in an app, | |
not a page in standalone mode */ | |
} | |
// Check if Mobile Safari on iPhone | |
if(isiOsSafari('iPhone')){ | |
document.write('Probably Safari on an iPhone: ' + ua); | |
} | |
// Check if Mobile Safari on iPod | |
else if(isiOsSafari('iPad')){ | |
document.write('Probably Safari on an iPad: ' + ua); | |
} | |
else{ | |
document.write('Probably something else: ' + ua) | |
} |
This still detect safari on Chrome ios. Any solution?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, this script above not work properly.
He detect as "safari on iphone" also the pages browsed with chrome from ios.
Any hint? Thank you!