Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

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?
Avatar of Martin Miller
Martin Miller
Flag of United States of America image

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"
}
Avatar of Aleks

ASKER

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.
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.
Avatar of Aleks

ASKER

Ok. I am testing now ..
Avatar of Aleks

ASKER

I tried with Chrome in a PC and it redirected me. Definitely not using Safari here.
Avatar of Aleks

ASKER

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

Avatar of Aleks

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Martin Miller
Martin Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Big Monty
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 :)
Avatar of Aleks

ASKER

Merci!  All good!  Hope you are doing well too. You know we are here 4u
Avatar of Aleks

ASKER

This is a variation of the code we used. Works good for what we need.