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?
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?
Share Improve this question edited Jun 21, 2010 at 3:44 Alex Angas 60k41 gold badges143 silver badges212 bronze badges asked Jun 7, 2010 at 11:02 blank bankblank bank 3751 gold badge2 silver badges10 bronze badges 15- 16 If you would just use JQuery, I'm pretty sure you could actually punch them in the face instead of only showing a picture of someone being punched in the face. – Mark Rushakoff Commented Jun 7, 2010 at 11:04
- 4 Umm...that's pretty heavy handed, blocking all Safari users and showing a picture of being punched in the face. Not that I should be using SO to promote my sociopolitical ideals, but I'm sure at least most people here would agree – Adam Batkin Commented Jun 7, 2010 at 11:06
- 18 You do know that not all Safari users are on the iPhone, right? – Tom Morris Commented Jun 7, 2010 at 11:07
- 5 I use Safari on the Mac and you know what? I don't want to visit your poxy game site anyway, so there! :-P – John Topley Commented Jun 7, 2010 at 11:10
- 5 By the way, there was a guy who ran some political blog who decided that Adblock users were thieves and blocked them from his websites, and encouraged others to do likewise. Pretty much everyone thought he was a moron. For good reason, I might suggest. – Tom Morris Commented Jun 7, 2010 at 11:24
3 Answers
Reset to default 25This 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();
}
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.
if(navigator.appName == "Safari")
{
....your code goes ....
}