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.
Related
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));
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.
thanks for reading.
I have a simple problem that i can't solve. I just need to have a html map with Jquery fadeIn and fadeOut effects. I got it and it works everywhere except with IE8.
So i just wanna add a pure javascript already tested and working just when IE8 is present.
Obviously i tried writing this code but is not working even with a simple alert after the IE comment.
Can you help please?
Here's the code, thanks!
<script type="text/javascript">var runFancy = true;</script>
<!--[if IE]>
<script type="text/javascript">
runFancy = false;
alert(runFancy);
function showIt(name)
{
document.getElementById('map_image').src='immagini/distributori/8/'.concat(name).concat('.png');
document.getElementById('map_image').style.display='block';
}
function hideIt()
{
document.getElementById('map_image').src='immagini/distributori/8/map.png';
};
</script>
<![endif]-->
Here's the code for other browsers
<!--[if !IE]><!-->
<script type="text/javascript">
//alert('hi'); not working even just this
if (runFancy) {
function showIt(name)
{
document.getElementById('map_image').src='immagini/distributori/single/'.concat(name).concat('_single.png');
$('#mapon').fadeIn(400);
}
function hideIt()
{
$('#mapon').fadeOut(300);
};
}
</script>
<!--<![endif]-->
even this is not working, any advice?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body style="background-color:red">
<h1>Ciao</h1>
<!--[if IE]>
<style language="text/css">
body{background-color:black;}
</style>
<![endif]-->
</body>
</html>
not working with every IE version.
This will help you:
HTML:
<!doctype html>
<!--[if IE]><![endif]-->
<!--[if lt IE 7 ]> <html lang="en" class="ie6"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en" class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html lang="en"><!--<![endif]-->
<head></head>
<body></body>
</html>
Javascript:
<!--[if IE 8]>
<script type="text/javascript" src="IE8.js">
<![endif]-->
the code you have should be working but if you want to target just IE 8 put IE 8 instead of just IE like so
<!--[if IE 8]>
do stuff
<![endif]-->
edit: removed depreciated jquery solution
Is there a way to use HTML conditional tags like <!--[if (IE 6)|(IE 7)]> inside a .js file as I need to execute certain javascript if visitor is using IE6 or IE7?
Thanks
I'd do something like this with the markup...
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class=""> <!--<![endif]-->
<head>
Then in your JS, you can check for the appropriate body styles:
if (html tag has class lt-ie8) {
// we're in ie7 and below land
}
You can use conditional comments in your markup, and use the result to create conditional variables in your js. Start your doc with this:
<!DOCTYPE HTML>
<!--[if lte IE 7]><html lang="en" class="ieDetect ie7Detect"><![endif]-->
<!--[if IE 8]><html lang="en" class="ieDetect ie8Detect"><![endif]-->
<!--[if IE 9]><html lang="en" class="ieDetect ie9Detect"><![endif]-->
<!--[if !IE]>--><html lang="en"><!--<![endif]-->
I usually prefer to use jQuery for this and check for classes:
var ieDetect = $('.ieDetect').length
, ie7Detect = $('.ie7Detect').length
, ie8Detect = $('.ie8Detect').length
, ie9Detect = $('.ie9Detect').length
;
if (ieDetect){
// do stuff with IE
}
If you want to only use JS, you can either use a class fetching function and retain the markup above, or you could forgo the generic ieDetect class and just use ie7Detect (and 8,9) as IDs on the html tag.
var ie7Detect = document.getElementById("ie7Detect").length
, etc...
Separate JS files are the best option.
However you can use navigator.userAgent:
var userAgent = navigator.userAgent;
From this you can determine the browser the user is using to view your page and use the if statement built into JS.
User navigator.userAgent to check the browser the user has.
Recommended reading: http://api.jquery.com/jQuery.support/
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>