Redirect if user is using Safari

Using Classic ASP, is there a reliable way to redirect a user to another page on load if the user is using Safari?
LVL 1
AleksAsked:
Who is Participating?
 
Martin MillerCTOCommented:
Your function modified...

function getBrowserName() {
    var name = "Unknown";
   
    if(navigator.userAgent.indexOf("Safari")!=-1){
        name = "Safari";
        window.location.href = "anotherPage.html"
    }
    return name;   // OR you can add the redirect to the calling of the getBrowserName(),
}

if( getBrowserName() == "Safari" ){
    alert("You are using Safari");
}
}
1
 
Martin MillerCTOCommented:
Yes, but how are you detecting the browser type ?

Are you doing something like this ?

var userAgent = navigator.userAgent.toLowerCase();
if(/safari/.test(userAgent) )  {
    window.location.href = "anotherPage.html"
}
0
 
AleksAuthor Commented:
Currently I don't have a script to detect the browser, we support most browsers, just not Safari.
Will the script above help redirect if the user is using Safari ONLY?

I have read that sometimes redirects will redirect when user is using Chrome, I just want to make sure this is not the case.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Martin MillerCTOCommented:
The javascript snippet is locking in on the User Agent identifier text string, Safari converted to lowercase text, as safari. I don't think Chrome will give out Safari emulation without modification.
0
 
AleksAuthor Commented:
Ok. I am testing now ..
0
 
AleksAuthor Commented:
I tried with Chrome in a PC and it redirected me. Definitely not using Safari here.
0
 
AleksAuthor Commented:
I found this script which seems to work fine, how can I change it so that it redirects when browser is Safari ?
And remove the alerts when its something else?


function getBrowserName() {
    var name = "Unknown";
    if(navigator.userAgent.indexOf("MSIE")!=-1){
        name = "MSIE";
    }
    else if(navigator.userAgent.indexOf("Firefox")!=-1){
        name = "Firefox";
    }
    else if(navigator.userAgent.indexOf("Opera")!=-1){
        name = "Opera";
    }
    else if(navigator.userAgent.indexOf("Chrome") != -1){
        name = "Chrome";
    }
    else if(navigator.userAgent.indexOf("Safari")!=-1){
        name = "Safari";
    }
    return name;   
}

if( getBrowserName() == "Safari" ){
    alert("You are using Safari");
}else{
    alert("You are surfing on " + getBrowserName(name));
}

Open in new window

0
 
AleksAuthor Commented:
This worked:

function getBrowserName() {
    var name = "Unknown";
    if(navigator.userAgent.indexOf("MSIE")!=-1){
        name = "MSIE";
    }
    else if(navigator.userAgent.indexOf("Firefox")!=-1){
        name = "Firefox";
    }
    else if(navigator.userAgent.indexOf("Opera")!=-1){
        name = "Opera";
    }
    else if(navigator.userAgent.indexOf("Chrome") != -1){
        name = "Chrome";
    }
    else if(navigator.userAgent.indexOf("Safari")!=-1){
        window.location.href = "notsupported.html"
    }
    return name;   
}

if( getBrowserName() == "Safari" ){
    window.location.href = "notsupported.html";
}else{
   
}

Open in new window

0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
no points please, just offering up a suggestion...

if you're ok with paying for something very reliable, then you may want to check out https://51degrees.com/device-detection, which is an online service that does browser detection (among MANY other things like browser details, not just the UserAgent string, which can change as new versions of browsers are released, screen resolution, and yes, these pieces of data can be obtained without the use of 3rd party services, but this makes it so much easier). I had to implement it for my job and it was a lot easier to use than I expected.

Hope all is well with you and the family :)
0
 
AleksAuthor Commented:
Merci!  All good!  Hope you are doing well too. You know we are here 4u
0
 
AleksAuthor Commented:
This is a variation of the code we used. Works good for what we need.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.