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 = "";
 //return "Windows Phone"; &amp;amp;nbsp;//you may choose to return value instead

if (/android/i.test(userAgent)) {
 window.location = "";

// iOS detection from:
 if (/iPad|iPhone|iPod/.test(userAgent) &amp;amp;amp;&amp;amp;amp; !window.MSStream) {
 return "iOS";

return "unknown";
window.onload = getMobileOperatingSystem;

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s