开发者

IF IE neglect/hide div

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 开发者_如何学PythonJavascript to work with.

How Can I achieve this ?

Thanks

  • Mandar


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".


jquery makes this easy:

if ( $.browser.msie ) {
  $("#container").css("display","none");
}


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.


Try:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜