I've been doing a lot of cross-browser testing and I must say that it is the bane of my existence. My beautiful fonts are sullied in particular browsers and particular environments. Despite having the exact same code in two separate browsers, content is rendered in entirely different ways. This is no secret, of course. But what I'm wondering is if there is a way to literally block access to an HTML page from users with particular browsers?
<!--[if IE/Safari/Firefox/Chrome/Opera/etc]>
<link rel="stylesheet" href="/stylesheets/block.css" type="text/css" />
<![endif]-->
开发者_StackOverflow社区block.css
body { display: none; }Professionally, I'm obligated to support all the browsers and environments that my users might feel they want to use. And it's really not so bad to learn how and why each browser acts the way that it does. But for side projects, it would be kind of nice to not have to worry about whether or not I will be shunned from society for not supporting browser x.
Also note that I didn't call out a particular browser as being particularly deficient, so please feel free to leave out any "browser x sucks!" comments.
There are several ways to do this:
- You could do this with your web server (I don't know what you're using)
- You could do this in your server code (again, not sure what you're using)
You could do this with JavaScript:
if(<BROWSER SUCKS>) { window.location = "YOUR-ERROR-PAGE.html"; }
Or
if(<BROWSER SUCKS>) {
alert("Your browser sucks, use 'xyz' instead.");
}
Your code would need to look at the window.navigator
object to detect the browser type.
Specifically with regards to your example above, to hide the page if the browser sucked, you would use:
document.body.style.display = "none";
Yes, probably, but it's not feasible as any modern browser will let you change the user-agent string and thus (likely) bypass your checks.
You could use something like:
<?php
$using_ie6 = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE);
if($using_ie6) {
die('Sorry, your browser is not supported');
}
?>
and modify according to which browser you wish to block, the above obviously would allow you to block IE6.
You can use http://www.useragentstring.com/ to help
精彩评论