Detect browser and redirect user

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 < in> Internet Explorer would be:

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

I'm waiting for a help, thank you!

    
asked by anonymous 05.11.2015 / 18:21

2 answers

3

You can use a regex to return true if Safari .

Example:

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

A more complete method for identifying 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";
}
    
05.11.2015 / 19:42