最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect Safari and redirect - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jan 29, 2014 at 18:16 Bojangles 102k50 gold badges174 silver badges209 bronze badges asked Jan 29, 2014 at 18:11 xeflipxeflip 251 gold badge3 silver badges10 bronze badges 10
  • Can you post a link to the site? – GSaunders Commented Jan 29, 2014 at 18:12
  • 1 Check: stackoverflow./questions/7944460/detect-safari-browser – Skwal Commented Jan 29, 2014 at 18:13
  • “I currently use this script to detect and redirect a page when safari is used” – what for? – C3roe Commented Jan 29, 2014 at 18:13
  • hi i currently cant post the link as this is private. but the problem im facing is that it redirects on both safari and chrome. how do i only detect and redirect in safari only – xeflip Commented Jan 29, 2014 at 18:13
  • 6 Maybe you should fix that content instead of trying to detect Safari? – Bojangles Commented Jan 29, 2014 at 18:16
 |  Show 5 more ments

4 Answers 4

Reset to default 3

My smart code is:

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

Try this simple code:

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

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>

Try this:

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

评论列表(0)

  1. 暂无评论