Detecting OS and Redirect to a specify site

There will be some instance where we need to redirect user to different site based on their platform. As such the following javascript will do the job to which OS the user is on. In this case, Android, iOS, and Windows (Windows Moble) is supported. The following code snippet will be inserted to the head section of your html page.

Lastly you would need to have window.onload = getMobileOperatingSystem to trigger the javascript after the page finished loading.


<script type="text/javascript">
 function getMobileOperatingSystem() {
 var userAgent = navigator.userAgent || navigator.vendor || window.opera;

// Windows Phone must come first because its UA also contains "Android"
 if (/windows/i.test(userAgent)) {
 window.location = "https://www.microsoft.com/store/apps/9nblggh4trj3";
 //return "Windows Phone"; &amp;amp;nbsp;//you may choose to return value instead
 }

if (/android/i.test(userAgent)) {
 window.location = "http://codeinstincts.xyz/apk/TVDrama.apk";
 }

// iOS detection from: http://stackoverflow.com/a/9039885/177710
 if (/iPad|iPhone|iPod/.test(userAgent) &amp;amp;amp;&amp;amp;amp; !window.MSStream) {
 return "iOS";
 }

return "unknown";
}
window.onload = getMobileOperatingSystem;
</script>

This entry was posted in Web Development and tagged , , , , , , , . Bookmark the permalink.

Leave a comment