Using javascript for client browser information

Posted By : Shakil Pathan | 14-Dec-2014

Hi Everyone,

In this blog, I am going to explain you about javascript code detecting client browser information and redirect accordingly.

By using this code you can detect that the browser runnig on which os or device and use the information for redirect in your function.

Here is the code you need:

isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i) ? true : false;
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i) ? true : false;
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i) ? true : false;
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
}
};

After you can get this information in your function and use accordingly like:

function redirection(){
if ( isMobile.Android() ) {
window.location.href = "market://details?id=yourPlayStoreAppId";
} else if ( isMobile.iOS() ) {
window.location.href = "https://itunes.apple.com/us/app/YourAppId";
}
}

Like this you can detect other browser information.

Thanks

Shakil

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..