Redirect works on Mac and PC but not on phone or ipad.

We are using the script below to redirect users who have Safari. Which our software does not support.
The script works perfectly on Mac OS computers and PC, but it redirects everyone when using ipad or iphone regardless to the fact they are using Firefox or Chrome.
How can we fix it so that it does not redirect on ipad/iphone unless they are using Safari?

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

LVL 1
AleksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin MillerCTOCommented:
You need to add an additional check for the operating system, something like the following, and add Phone OS

function detectOS(){
   if (navigator.userAgent.indexOf("Win")!=-1) return "Windows";
   if (navigator.userAgent.indexOf("Mac")!=-1) return "MacOS";
   if (navigator.userAgent.indexOf("Linux")!=-1) return "Linux";
   if (navigator.userAgent.indexOf("OpenBSD")!=-1) return "OpenBSD";
   if (navigator.userAgent.indexOf("FreeBSD")!=-1) return "FreeBSD";
   if (navigator.userAgent.indexOf("NetBSD")!=-1) return "NetBSD";
   return undefined;
}

Open in new window


I found this snippet that might help
//iPhone Version:
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    window.location = "http://newlocation";
}
//Android Version:
if(navigator.userAgent.match(/android/i)) {
    window.location = "http://newlocationi";
}
//Blackberry Version:
if(navigator.userAgent.match(/blackberry/i)) {
    window.location = "http://newlocation";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
Right. That will give me the os. But I need the browser type which apparently my script doesn't get properly when using iPad.
I need to redirect if user has safari regardless of OS. So not sure getting the OS helps.
0
Martin MillerCTOCommented:
Okay, I how this helps solve that final pattern missing :-)  Catch the part about forcing lower case and text search match

navigator.userAgent.toLowerCase(); if (ua.indexOf('safari') != -1)
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

AleksAuthor Commented:
I am sorry I am not really a programmer. Where should I add that line into my code?
0
Martin MillerCTOCommented:
You need to merge the logical checks together for OS/Phone, and browser.

e.g. if(iphone AND safari) then do this...
0
AleksAuthor Commented:
That makes sense. Any way I can get some help with that?
0
Martin MillerCTOCommented:
something like this:


if(  ( getBrowserName() == "Safari")   &&  navigator.userAgent.match(/iPhone/i)  ){

// The redirect to somewhere else...
 window.location.href = "notsupported.html";
}

Open in new window

0
AleksAuthor Commented:
I think what we need is to avoid redirecting if its iphone or ipad and the they are using Chrome or Firefox, which are being redirected and should not. Safari does not need extra code to redirect it already does.
0
Martin MillerCTOCommented:
Can we close this question ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.