Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Grant Bates
Grant Bates
1,915 Points

Looking to create a link that will recognize device and send to the proper app store.

I have been searching around the web for a simple and lightweight piece of code that will recognize the device being used and send the user to the proper app store.

I feel like this is a pretty common bit of coding but for the life of me I can't find a tutorial. Any help would be appreciated.

I would prefer to just use HTML5 and CSS if possible but a little javascript would kill me. (I don't know much about scripting.)

If you don't have the answer but know where I might find one please share.

Thank you.

So far I have this going for me...

var ua = navigator.userAgent; 

if (ua.indexOf("Android")>-1) {
  // generate play store link
} else if (ua.indexOf("iPad")>-1 || ua.indexOf("iPhone")>-1) {
  // generate ios link
} else if (ua.indexOf("Windows Phone")>-1) {
  // generate windows store link
}

But I don't know how to incorporate with the HTML

1 Answer

xyraine
xyraine
1,669 Points

Hi Grant,

You might be interested in reading more into to filtering user agents. The best way would have to be through JS. I would do a search for "How to detect user agent using javascript".

I'm not an expert yet but I hope this helps! Ryan

Grant Bates
Grant Bates
1,915 Points

Thanks. I am finding lots of references to identify the device but not a lot on what to do after the fact.