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?
 
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
 
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
Cloud Class® Course: CompTIA 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
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.