xeflip
xeflip

Reputation: 25

Detect Safari and redirect

I currently use this script to detect and redirect a page when safari is used

if(/safari/.test(navigator.userAgent.toLowerCase())) {
    window.location.href = "elsewhere.html"
}

however it redirects in safari and chrome.

how do I make it redirect only in safari only?

Upvotes: 1

Views: 10676

Answers (4)

user3246718
user3246718

Reputation:

My smart code is:

var uagent = navigator.userAgent.toLowerCase();
if(/safari/.test(uagent) && !/chrome/.test(uagent))
{
    window.location.href = "elsewhere.html"
}

Upvotes: 3

Ashish Kumar
Ashish Kumar

Reputation: 3039

Try this:

if(typeof navigator.vendor!='undefined' && navigator.vendor.toLowerCase().indexOf('apple')!=-1){
 // redirection
}

Upvotes: 0

user3087089
user3087089

Reputation:

Try this simple code:

if(/safari/.test(navigator.userAgent.toLowerCase()) && !/chrome/.test(navigator.userAgent.toLowerCase()))
{
    window.location.href = "elsewhere.html"
}

Upvotes: 1

xeflip
xeflip

Reputation: 25

Just put this script into your header. It should detect and redirect safari only. Just change window.location.href = "somewhere.html" to the url you want to redirect safari users.

<script>
var ua = navigator.userAgent.toLowerCase(); 
 if (ua.indexOf('safari')!=-1){ 
   if(ua.indexOf('chrome')  > -1){

   }else{
    window.location.href = "somewhere.html" // saf
   }
  }

</script>

Upvotes: 0

Related Questions