简体   繁体   中英

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?

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();
 }
if(navigator.appName == "Safari")
{
....your code goes ....
}

More than a couple of lines but you could very simply cut this down so it only bothers with Safari: http://www.quirksmode.org/js/detect.html

A slightly more solid method would be to use a server-side detection method like php's get_browser([string $user_agent [, bool $return_array = false ]])

Needless to say, this is all a bit silly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM