IE detection not working - javascript

I wanted to detect IE8 and 9 via JavaScript. All of them return as 7.
I tried JQuery-1.8.2, but same results.
It returns "Compatible" version or something.
If both IE8 and 9 versions use their IE version as 7, why they behave differently?
Why MS labels it as 8 and 9 and send some other version details on the IP header?. Is this the expected behavior of IE?
I tried this too.
http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx
Can anyone give an explanation about this.
Thanks.

You can find a solution Detect IE version (prior to v9) in JavaScript
See:
<!doctype html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>
and then:
(function ($) {
"use strict";
// Detecting IE
var oldIE;
if ($('html').is('.ie6, .ie7, .ie8')) {
oldIE = true;
}
if (oldIE) {
// Here's your JS for IE..
} else {
// ..And here's the full-fat code for everyone else
}
}(jQuery));

Related

What <!--[if IE 6]> <![endif]--> does in ASP.Net's asp file? [duplicate]

This question already has answers here:
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
(4 answers)
Closed 8 years ago.
I saw a piece of code like this:
<!--[if IE 6]>
<script type="text/javascript">
function pageLoad() {
//$find('mpeIeWarning').show();
document.getElementById('divIE6').style.display = "";
}
</script>
<![endif]-->
Is
<!--[if IE 6]>
<![endif]-->
a comment tag? or doing something special, to be honest, I never used this thing.
It adds the script only if the browswer is in fact Internet Explorer 6. It's is a special type of comment, called conditional comment, you can read about it in here
http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx

How to detect if a user is using IE and inform them that the site doesn't support IE?

I'm creating a website with Zepto, so it doesn't support any Internet Explorer version.
How can I detect if a user is using Internet Explorer and redirect them to a page informing that the website doesn't support IE?
I've read about conditional comments, but them aren't supported in Internet Explorer 10.
Thanks.
You can never know for sure, but an easy option is to look at the user-agent (in e.g. PHP).
Also take a look here: How to display browser specific HTML?
Setup the document like this:
<!doctype html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>
For IE10 use this:
if (Function('/*#cc_on return document.documentMode===10#*/')()
){
document.documentElement.className+=' ie10';
}
Then, what you do with these classes depends on whether you want to just show/hide a div with css, or use javascript to redirect the page.
Javascript:
(function ($) {
"use strict";
// Detecting IE
var IE;
if ($('html').is('.ie6, .ie7, .ie8, .ie9, .ie10')) {
IE = true;
}
if (IE) {
// redirect
window.location.replace('http://www.myotherpage.com');
}
}(jQuery));
OR CSS:
.ie6 .myDivClassName,
.ie7 .myDivClassName,
.ie8 .myDivClassName,
.ie9 .myDivClassName,
.ie10 .myDivClassName {
display: block;
}
Depends on what you are trying to do.

getting the ie edition without calling jquery

hi first (Jezen Thomas ty your code help me, but i cant use it), i have to make this code work with out calling jquery (since we dont know if it will be supported), so basically i have to get this code to show the ie version, only using html and javascript
<html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]--
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>
<title>A HTML document needs a title to be valid.</title>
</head>
<body>
<script type="text/javascript">
window.onload = function mytest(){
"use strict";
// Detecting IE
var oldIE
if ($('html').is('.ie6, .ie7, .ie8')) {
oldIE = 1;
}
if (oldIE == 1) {
document.write("version<ie9")
} else {
document.write(" version => ie9")
}
}
</script>
</body>
</html>
i have to remove the ($) but i dont know for what to replace it (i just recently started with javascript)
alert(navigator.userAgent);
I hope that give you the start you need.

java script button not showing

ok i have this code (which i got from paul irish page+ some modifications) (it gives you your ie version (standar stuff):
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>
</head>
<body>
<h1>test:</h1>
<script type="text/javascript">
$(function test()) {
"use strict";
// Detecting IE
var oldIE
if ($('html').is('.ie6, .ie7, .ie8')) {
oldIE = true;
}
if (oldIE) == true {
document.write("version<ie9")
} else {
document.write(" version => ie9")
}
}
</script>
<script type="text/javascript">
<button type="button" onclick="test()">testing</button>
</script>
</body>
</html>
the thing is it isnt showing the testing button
The <button> element should not be surrounded by <script> tags. After all, it's HTML, not JavaScript.
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>
<title>A HTML document needs a title to be valid.</title>
</head>
<body>
<button id="test">I feel so clickable today.</button>
<!-- You must include jQuery before you can use its methods -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
"use strict";
(function ($) {
// Assignment with the ternary operator
var oldIE = ($('html').is('.ie6, .ie7, .ie8')) ? true : false;
// You need at least jQuery 1.7 to use .on()
$('#test').on('click', function() {
console.log(oldIE);
});
}(jQuery));
</script>
</body>
</html>
N.B. I included jQuery because I saw a $ symbol in your code.
Since you should be using the console during development, here's a snippet that safeguards against JS errors in browsers that don't provide a console.
// Avoid `console` errors in browsers that lack a console
(function(){var e;var t=function(){};var n=["assert","clear","count","debug","dir","dirxml","error","exception","group","groupCollapsed","groupEnd","info","log","markTimeline","profile","profileEnd","table","time","timeEnd","timeStamp","trace","warn"];var r=n.length;var i=window.console=window.console||{};while(r--){e=n[r];if(!i[e]){i[e]=t}}})();
But if you wanted to keep it in the script tag you would need to modify your code like so:
<script type="text/javascript">
document.write('<button type="button" onclick="test()">testing</button>');
</script>

Detect the Browser and show a other content

I build a website only with CSS3 & HTML5.
So IE can't display it correct.
I want to show every Internet Explorer-User a other site with a simple content: Please use a modern browser like Google Chrome, Fire Fox ...
How can i detect the browser and show a other site?
Or can i build a text for the Internet Explorer users like
<div style="display:none;">Please use a modern Browser!</div>
and let show it only for Internet Explorer-Users?
You can utilize IE detects that IE itself puts out. So place the div with the display none, and then make an ie stylesheet and target all the different versions you would like.
More information on specifics here: http://css-tricks.com/how-to-create-an-ie-only-stylesheet/
there is better option instead of adding a new style sheet
<!doctype html>
<!--[if lt IE 7 ]> <html class="ie ie6" lang="en-US"> <![endif]-->
<!--[if IE 7 ]> <html class="ie ie7" lang="en-US"> <![endif]-->
<!--[if IE 8 ]> <html class="ie ie8" lang="en-US"> <![endif]-->
<!--[if IE 9 ]> <html class="ie ie9" lang="en-US"> <![endif]-->
<!--[if gt IE 9]><!-->
<html lang="en-US">
<!--<![endif]-->
and in style sheet just add a class for selector
.page {
//some style for morden browsers
}
.ie .page {
//some style for all ie versions
}
.ie6 .page {
//some style for ie 6 only
}
.ie7 .page {
//some style for ie 7 only
}
.ie8 .page {
//some style for ie 8 only
}
.ie9 .page {
//some style for ie 9 only
}
it would be done using a single style sheet
navigator.userAgent
there is a very cool script here: http://www.quirksmode.org/js/detect.html
You can use the following :
<?php
$u_agent = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/MSIE/i', $u_agent))
{
// Change http://www.othersite.com, by your other site URL
header('Location: http://www.othersite.com');
}
?>
You can also use conditional comments for IE.

Categories