javascriptbrowsersafari

How do I block Safari users from my website?


I would like to block all users of Safari from visiting my flash game web site. I would like them to see a picture of someone being punched in the face instead of the games.

My understanding is that you can use JavaScript to do it, but I don't want to use some heavy framework like JQuery. Is there a way to do it in like a single line or two of JavaScript?


Solution

  • This is a horrible, horrible idea IMO. I can understand the sentiment, but this is going to do as much good, and raise as much sympathy, as sites with "Stop using IE, moron!" messages. But it's up to you....

    Quirksmode has a small BrowserDetect library that I trust has all the quirks worked out. If I were you, I would use that.

    To do it in one line, look for Safari in the navigator.userAgent string.

    Example code:

    if (navigator.userAgent.indexOf('Safari/') != -1){
     alert("Safari detected");
    }
    

    If you want to make 100% sure you catch them all (well, 99% bearing in mind the user agent string can be changed freely by the client), you'd need to use a server-side language like PHP.

    if (strstr($_SERVER["HTTP_USER_AGENT"], "Safari")) 
     {
      header("location:no-safari.html");
      die();
     }