IF IE neglect/hide div - javascript

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

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();

Related

Detect if any kind of IE (MSIE) [duplicate]

This question already has answers here:
Internet Explorer 11 detection
(12 answers)
Closed 8 years ago.
I dont want to allow users to access my site with Microsoft Internetexplorer (ANY VERSION).
What I´ve found so far was to detect if it´s lower or equal version 10.
A very annoing thing: Internetexplorer >v10 doesn´t admit to be a InternetExplorer.
What i´ve found and tried so far:
if(navigator.appVersion.indexOf("MSIE")!=-1){
alert("You use IE. That´s no good.");
}
or
if ( $.browser.msie ) {
alert( $.browser.version );
}
and
http://msdn.microsoft.com/en-us/library/ie/ms537509%28v=vs.85%29.aspx
I would use any solution if it is in javascript, jquery or php if there is one.
This works for me to detect any Version of the IE 5-11 (Internet Explorer) (Aug/05/2014):
if (navigator.appName == 'Microsoft Internet Explorer' || !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie == 1))
{
alert("Please dont use IE.");
}
This is because each release of Internet Explorer updates the user-agent string.
MSIE tokens have been removed in Internet Explorer 11 and $.browser uses navigator.userAgent to determine the platform and it is removed in jQuery 1.9.
You can use following code to determine the browser with pure java-script.
var isIE = !!navigator.userAgent.match(/Trident/g) || !!navigator.userAgent.match(/MSIE/g);
if(isIE){
alert("IE");
}
else{
alert("Not IE");
}
Thanks!
if you are not interessted wich version of ie the user currently use you can try get it work with detecting if the browser supports the Conditional Compilation Statements
http://msdn.microsoft.com/en-us/library/7kx09ct1%28v=vs.80%29.aspx
if(/*#cc_on!#*/false)
{
// You use IE. That´s no good.
alert("oh my god");
}
You can use conditional compilation
, e.g.
<script>
var isIE = false;
/*#cc_on isIE = true; #*/
</script>
But note that IE11 doesn't observe this in Standards Mode. User Agent sniffing is generally a bad idea, but as IE becomes more standards-compliant, it also becomes harder to detect (hopefully also meaning less need to)
For IE> 10 which is currently IE 11, user-agent carries something in Browser's HTTP request headers
Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
You can put a check on "rv:11.0" for version 11. Let me know if you need code for this.
I've found (maybe in SO) in the past this script and it worked for me (IE 10 too)
<![if IE]>
<script type='text/javascript'>
if(/*#cc_on!#*/false)
var bIsIE = 1;
</script>
<![endif]>
and then
if (typeof (bIsIE) != 'undefined')
{
//IE :(
}
else
{
//NOT IE :)
}

.find(".class:first") behavior with Opera and Safari

I'm using Soundmanager2 to play some audio files in a web site, but not using Flash.
It works fine with Firefox and Chrome, as they support ogg and mp3 respectively. However, it doesn't work with Opera 12.16. Theoretically, it supports ogg, and pass the condition if( supports_ogg_audio() ):
It is returning 1 in this function:
function supports_ogg_audio() {
var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));
}
So it detects ogg support. But as I'm doing:
currentRow = thisPlayer.find(".total-row:first");
I get this error from the Opera console:
Unknown pseudo class
[id='total-playlist'] .total-row:first
So I'm guessing that this is the problem. How could select the first thisPlayer.find(".total-row") element with better browser compatibility?
It neither works in Safari5+ and IE9+
You need to use first-child selector instead of first. See information here.

What headers or javascript do I add to the HTML to reject older browsers?

Developing a frontend realtime javascript application, we have decided to drop support for older browsers since it takes too much effort to support them.
What header or javascript should we add in the HTML, so that when they hit the URL, I can redirect them a page to obtain newer browsers before proceeding to our application?
you can use following properties of navigator object
navigator.appCodeName
navigator.appName
navigator.appVersion
navigator.cookieEnabled
navigator.platform
navigator.userAgent
you can also use jquery browser object
http://api.jquery.com/jQuery.browser/
The better option is to use feature detection. For example, to test if a browser has geolocation support, you can use the following:
if (navigator.geolocation)
{
// interact with geolocation features
}
If you insist on using browser detection instead, however, you can use the following to detect if the browser is using IE 8 or below:
function getInternetExplorerVersion()
// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
var ver = getInternetExplorerVersion(); // example: 8.0
var belowIE8 = ver <= 8.0;
Found here: http://www.mkyong.com/javascript/how-to-detect-ie-version-using-javascript/
If you want to determine this based on a particular JavaScript version, see below code (untested):
<script language="javascript1.5">
var supported = true;
</script>
<script>
if ('undefined' === typeof supported) {
alert('not at least 1.5 supported');
}
It's still better to do feature detection though, as that ties in better with what you're going to use it for.

Cannot access document's title element with jQuery (IE 8)

I'm seeing this issue in Internet Explorer 8, but not in Safari or Firefox. So far, I have not tested in other IE versions.
I am developing my own jQuery plugin and, for this question, I've stripped it down to the two relevant lines.
In IE 8, using the code below, $('title').text() does not do anything. docTitle is blank because title is blank, as if the jQuery selector for <title>, $('title') is not working. (Again, AFAIK, this is just in IE 8)
(function ($) {
$.fn.myPlugin = function (options) {
var title = $('title').text(),
docTitle = escape(title);
};
})(jQuery);
http://jsfiddle.net/sparky672/YMBQ2/
However, using the plain JavaScript code below, document.title is working fine in everything including IE 8...
(function ($) {
$.fn.myPlugin = function (options) {
var docTitle = escape(document.title);
};
})(jQuery);
EDIT:
It does not matter that this code is inside a plugin.
Same result in IE 8 with this...
$(document).ready(function () {
var title = $('title').text();
alert(title);
});
Just to clarify, I am not insisting on using this. In fact, I fixed my plugin by simply using document.title instead. If it wasn't clear initially, I'm just asking why this does not work in IE 8.
Can anyone explain why, or what stupid mistake I may have made here?
EDIT 2:
Here are some jQuery Bug reports on this issue
http://bugs.jquery.com/ticket/7025
http://bugs.jquery.com/ticket/5881
http://bugs.jquery.com/ticket/2755
And dozens of others reporting the same thing. The official response is to state, "document.title is the only reliable cross-browser way and should be used instead" and the Ticket is closed. So there you go.
I guess jQuery iterates over all TextNodes and concatenates its nodeValue. IE stores this value differently than other browsers.
var title = document.getElementsByTagName('title')[ 0 ];
title.firstChild // This would be the Text-Object with the characterdata of the title
// Firefox: [object Text]
// IE: null
This should be the reason you cannot get the textContent with jQuery.text(). title.text seems to be cross browser comp. I only tested it in IE 7 and Firefox 3.6 but you can check the other browser if you like. But why not using document.title?
try using $('title').html() which should work in all browsers

What is the correct way to detect Opera using jQuery?

Amazon.com recently updated their javascript, and it's causing problems with some Opera browsers.
Their browser detection code looks like so, but it's faulty:
function sitbReaderIsCompatibleBrowser() {
if (typeof(jQuery) == 'undefined') {
return false;
} else {
var version = jQuery.browser.version || "0";
var splitVersion = version.split('.');
return (
(jQuery.browser.msie && splitVersion[0] >= 6) // IE 6 and higher
|| (jQuery.browser.mozilla && (
(splitVersion[0] == 1 && splitVersion[1] >= 8) // Firefox 2 and higher
|| (splitVersion[0] >= 2)
))
|| (jQuery.browser.safari && splitVersion[0] >= 500) // Safari 5 and higher
|| (jQuery.browser.opera && splitVersion[0] >= 9) // Opera 5 and higher
);
}
}
Nothing obviously wrong jumps out at me with this code, but I've never used jQuery before so I don't know.
Even though this code looks like it's attempting to let Opera users through, when I visit the page with Opera 9.64 I get an "unsupported browser" message. If I change Opera's settings to report itself as Firefox, the page works perfectly! With that in mind, I'm pretty sure it's a problem with the script and not the browser.
Any jQuery experts have a suggestion?
You can replicate the behavior by visiting any book on Amazon and clicking the "look inside this book" link.
Prior to jQuery 1.3, you could use jQuery.browser:
if( $.browser.opera ){
alert( "You're using Opera version "+$.browser.version+"!" );
}
From version 1.3, you should use jQuery.support instead.
Main reason for this is that should should avoid checking for browsers, as features may change from version to version, making your code obsolete in no time.
You should always try to use feature detection instead. This will allow you to see if current browser supports the feature you're trying to use, regardless the browser brand, version, etc.
There is a special window.opera object which is present in all Opera 5+ browsers. So something as simple as:
if (window.opera && window.opera.buildNumber) {
// we are in Opera
}
would be enough.
I check for Opera like this:
if (/Opera/.test (navigator.userAgent)) // do something
Why would you want jQuery?
It is much better to detect javascript capabilities rather than browser userAgent.
ie DOM, XmlHttpRequest, eventing model (event.target vs event.srcElement), ActiveX, Java etc
By focusing on the API functions that you will require, rather than a target browser you will create a more robust set of scripts, and inevitably less special casing.
This link here at opera will probably tell you more
A very simple way from Opera themselves:
if (window.opera) {
//this browser is Opera
}
Source: http://my.opera.com/community/openweb/idopera/
The main reason why Amazon fails on Opera is because the send different code from the server side already... If you visit the same page with Firefox and then save that page and reopen it in Opera it works fine...
But they promised to fix that sometime in January...
I think this way is the best
if ( window.opera.version() == 12) {
}
This example check if opera version is 12. Very useful when I have problems with font-face in Opera.
I don't know for sure ( i never really check for opera anyway) but if the built-in jQuery functionality doesn't detect opera, may be a bug with the jQuery which needs to be fixed. I would suspect if that's the case, it should get resolved fairly quickly.
In current HTML5 times, you can also check for browser features instead often.
if (!window.FormData) { alert("xmlhttprequest L2 FormData interface not available"); }

Categories

Resources