Detect browser and redirect user

Asked

Viewed 2,573 times

3

I have an animation problem in Safari and would like to know how to restrict the use of Safari of all versions without having to list one by one, for example if I were to redirect a version of Internet Explorer would be:

<!--[if IE 7]> <script type="text/javascript">
window.location = "http://www.http://browsehappy.com/";

I’m looking forward to some help, thank you!

2 answers

3

You can use a regex to return true if it is Safari.

Example:

var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
console.log(isSafari);

3

A more complete method to identify any browser would be with the following function:

navigator.sayswho = (function(){
var ua= navigator.userAgent, tem, 
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
if(/trident/i.test(M[1])){
    tem=  /\brv[ :]+(\d+(\.\d+)?)/g.exec(ua) || [];
    return 'IE '+(tem[1] || '');
}
M= M[2]? [M[1], M[2]]:[navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
return M.join(' ');
});

//"Chrome 46.0.2490.80"

And in your case:

if (navigator.saywswho().toLowerCase().indexOf("safari") != -1) {
    window.location = "http://google.com/chrome";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.