Hello everyone! I want to create site. When user entering on my website from iOS, i'll show him a link to app my website in App Market . When user entering on my website from Android, i'll show him a link to app my website in Play Market. I searched the internet for examples and found only one https://iq-option.trade/ Please, help me.
A quick google comes up with a few choices. This one looks good. var isMobile ={ Android:function(){ return navigator.userAgent.match(/Android/i); }, BlackBerry:function(){ return navigator.userAgent.match(/BlackBerry/i); }, iOS:function(){ return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera:function(){ return navigator.userAgent.match(/Opera Mini/i); }, Windows:function(){ return navigator.userAgent.match(/IEMobile/i); }, any:function(){ return(isMobile.Android()|| isMobile.BlackBerry()|| isMobile.iOS()|| isMobile.Opera()|| isMobile.Windows()); } }; if(isMobile.any()){ window.location ="#";} Code (javascript):
Can't say I'm wild about it, but that's because if you call any of the methods more than once, particularly the "any" method, it becomes inefficient since it's running it every time, instead of just determining it once at the start. Be more useful if it just set a variable on startup. var isMobile = navigator.userAgent.match(/Android/i) ? 'Android : ( navigator.userAgent.match(/BlackBerry/i) ? 'Blackberry' : ( navigator.userAgent.match(/iPhone|iPad|iPod/i) ? 'iOS' : ( navigator.userAgent.match(/Opera Mini/i) ? 'Opera' : ( navigator.userAgent.match(/IEMobile/i) ? 'Windows' : false ) ) ) ); if (isMobile == 'iOS') { // add your IOS button here } Code (markup): For "any" just "if (isMobile)"... and for none of course it's !isMobile. Lets us leverage JS' loose comparison for something good. For a change...
How reliable this thing is and why would anyone want to do this? Isn't it much better to just make your site responsive / fluid and then show buttons to appropriate apps in the footer?
Normally I'd agree with you 100%, but given he's linking to the different stores for each platform, it's probably not a horrible idea to have these (optional?) links generated in the scripting to the appropriate one. No sense having the link to Google Play show up on the iPhone, or having the App store show up on Android... or the Microsoft store for either. (a third option) Usually browser sniffing is a bad thing, but this is one of those rare cases where IMHO it makes more sense than showing the links when they just don't apply for the user. That said I'd show both links on Opera Mini, since it could be EITHER platform.
Agreed, it's rare that code snippets get used "as is". Definitely a good idea to do it once and store the result.