views:

53

answers:

4

Hello,

I have page like this

<html>
<head>

<div id="container">
Hello, this is NON-IE content.
</div>

</head>
</html>

What I want is to Stop Internet Explorer [ IE ] from loading OR Neglecting <div id="container"> & allow all other browsers to load It.

Don't want to use <!--[if IE 7]>.

Looking for Javascript to work with.

How Can I achieve this ?

Thanks

  • Mandar
+4  A: 

jquery makes this easy:

if ( $.browser.msie ) {
  $("#container").css("display","none");
}
David
MANnDAaR
-1 even jQuery says: "We recommend against using this property, please try to use feature detection instead (see jQuery.support)." source: http://api.jquery.com/jQuery.browser/
galambalazs
@galambalazs: He did not ask how to detect if the browser support a specific feature, but if it is IE. His answer is correct.
William
Do you know how jQuery gets browser? **"read from navigator.userAgent"** Do you know how navigator.userAgent starts in every browser (except opera)? Let me tell you. It starts with: Mozilla... **Rule no.1: BROWSERS LIE**
galambalazs
galambalazs: It checks if MSIE is contained in the string, which works. Obviously someone could spoof it.
William
+1 because it actually answers the question.
Paperjam
+1 as I agree with @Paperjam.
William
I know it contains MSIE in "most" cases, but the thing i wanted to highlight is that user agent string cannot be trusted since the browser wars has started...
galambalazs
And the reason for that is the kind of usage we can see here. Browsers lie to get the content...
galambalazs
A: 

if($.browser.msie) $("#container").remove();

spinon
+2  A: 

For users that don't want to use jQuery, you can simply do:

if (/*@cc_on!@*/false) {
    alert('This is Internet Explorer!');
}

http://devoracles.com/the-best-method-to-check-for-internet-explorer-in-javascript

What we see up there? I declared a new variable, called IE, which has the value a comment block followed by ‘false‘. The above variable will be understood by IE: var IE = !false, because Internet Explorer uses JScript — a Javascript-like dialect of the standard ECMAScript — instead of Javascript which is used by all the other browsers. JScript can parse the comments, just like Internet Explorer (see conditional HTML comments post). This is a unique feature of IE, none of the other browsers can do it, so Firefox, Chrome, Safari, Opera, all will understand the above declaration as IE = false.

Note: If any other browser were to use "JScript" this would pass, but since JScript is written by Microsoft I think you're safe. Another method is the navigator object, which you can pull the application name. Although some applications like to spoof this, so I believe the JScript is a bit more reliable.

if (navigator.appName == 'Microsoft Internet Explorer') {
    alert('This is Internet Explorer!');
}

Edit: This was more to help users in detecting IE, not about directly answering the users question. Also, for users not wanting to use jQuery you could simple do document.getElementById('container').style.display = 'none'; -- Just figured I'd add this in since my post did mention "without using jQuery".

William
@william : +1 Thanx for this. better way to check IE.
MANnDAaR
This is a fragile hack that relies on behavior that could easily be changed in a later version of IE. Using the user agent string would be better if you dont want to use Jquery.
David
@David: Agree, although there is really no perfect solution when using JavaScript, unless you used JavaScript with a combination of the IF tags. user agents get spoofed a lot, and this "hack" could be changed by Microsoft.
William
A: 

document.all

A very dirty option is using document.all which IE supports.

if(document.all){
    document.getElementById("container").style.display = "none";
}

For some crazy reason, Chrome does support document.all, but checking for document.all returns false in it.

navigator

Another option is to look at the navigator object.

if (navigator && navigator.appName == 'Microsoft Internet Explorer'){
    document.getElementById("container").style.display = "none";
}

This could fail on browsers that spoof themselves as other browsers.

comparison 'feature'

You could also use a simple comparison.

if('\v' == 'v'){
    document.getElementById("container").style.display = "none";
}

I personally would not do this, but it is a neat detection to list.

epascarello