So i am going to add a redirect to my site to toss every one that is using ie 7 or lower off to a different page and came up with this JavaScript, but it seems to have stopped working.
<script type="text/javascript">
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (ieversion<=8)
window.location = "ie.html"
}
window.location = "main.html"
</script>
Your code is always resulting to having gone to main.html. Even when the code falls into <8, you'll fall out of the if into setting to main.
Consider refactoring by either:
setting a return after setting to ie.
or
var redir="main.html";
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
{
var ieversion=new Number(RegExp.$1);
if (ieversion<=8)
{
redir = "ie.html";
}
}
window.location = redir;
Check out conditional comments.
So you can do something like:
<script type="text/javascript">
<!--[if (!IE)|(gt IE 7)]>
window.location = "ie.html"
<![endif]-->
<!--[if lt IE 8]>
window.location = "main.html"
<![endif]-->
</script>
Conditional comments (as suggested by #Kon) are the way to go. Here's a working implementation:
<script type="text/javascript">
var ie7OrLower = false;
</script>
<!--[if lte IE 7]><script type="text/javascript">
ie7OrLower = true;
</script><![endif]-->
<script type="text/javascript">
window.location = ie7OrLower ? "ie.html" : "main.html";
</script>
You can test it with this regular expression: (MSIE\ [0-7]\.\d+)
Here is a JavaScript example on how to use it:
if (/(MSIE\ [0-7]\.\d+)/.test(navigator.userAgent)) {
// do something
}
I've always used Quirks Mode's BrowserDetect.js for my browser detection needs. Check it out - http://www.quirksmode.org/js/detect.html
Once you've referenced the .js file, you can access lots of information:
//Browser Name
BrowserDetect.browser
//Browser Version
BrowserDetect.version
//Operating system
BrowserDetect.OS
I'd just use the examples at http://www.ie6nomore.com/
Related
I have a WordPress website and I have installed one plugin and the plugin has some problems in IE6 browser.
So, I want to disable that jQuery plugin when the page is viewed with the IE6 browser.
So now I need a jQuery statement to disable ALL other statements that are loading from other JS files.
use Downlevel-revealed conditional comments :
<!--[if lte IE 6]><![if gte IE 7]><![endif]-->
<!-- keep your script in between these two comments,
it will work in all browser except ie -->
<!--[if lte IE 6]><![endif]><![endif]-->
Explained here : Hiding some HTML from IE6?
I'm not entirely sure why IE6 is even on your agenda, but to each their own.
If it were me I would write something like this.
<!doctype html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->....
.....
(function ($) {
"use strict";
// Detect IE 6
var greatGreatGranddadsBrowser;
if ($('html').is('.ie6')) {
greatGreatGranddadsBrowser = true;
}
if (greatGreatGranddadsBrowser) {
// Remove the elements that you don't want loaded
// Tell the users to seriously consider coming into the real world
} else {
// Do whatever else you need to do
}
}(jQuery));
Trying to get a page to load a different js file if the browser is IE, but a different one if it is any other browser. I've corrupted this, but it won't work, does anyone have any ideas?
Any help is appreciated:
<script type="text/javascript">
var ie = false;
</script>
<!--[if IE]>
<script type="text/javascript">
ie = true;
</script>
<![endif]-->
<script type="text/javascript">
if(ie == false)
{
document.write ("<script src="js/moreskins.js" type="text/javascript">")</script>;
} else {
document.write ("<script src="js/ieskins.js" type="text/javascript">")</script>;
}
</script>
Looks like your last script tag is a little jumbled, plus as Teemu mentioned, IE10 does not support conditional HTML comments.
If you really need to target IE, I would check the user agent for "MSIE":
<script type="text/javascript">
var ie = !(navigator.userAgent.indexOf("MSIE")<0);
if(ie == false) {
document.write ("<script src=\"js/moreskins.js\"></scr"+"ipt>");
} else {
document.write ("<script src=\"js/ieskins.js\"></scr"+"ipt>");
}
</script>
Use conditional comments to load file in IE and ignore in other browsers.
<!--[if IE]>
<script src="js/ieskins.js" type="text/javascript">
<![endif]-->
<![if !IE]>
<script src="js/moreskins.js" type="text/javascript">
<![endif]>
Note: the else part (<![if !IE]>) which is not a comment. So for IE it is else part and for other browsers, it is nothing.
EDIT
you can also try the following instead of document.write
var script = document.createElement('script');
script.src = "js/moreskins.js";
document.getElementsByTagName('head')[0].appendChild(script);
The problem here is the closing script tag. When using document.write() to add scripts, you need to cut end tag into pieces. Something like below (notice the fixed quoting and parenthesing too).
document.write('<script src="js/moreskins.js" type="text/javascript"></scr' + 'ipt>');
Script execution is stopped, when parser founds the first literal end tag, that's why you need to cut it in the argument.
Also notice, that IE10 doesn't support conditional comments, you should rather use feature detection instead of browser detection.
Thought I'd take a stab at writing some code too ;-)
<script>
(function(){
var script = document.createElement("script"),
is_ie = (/MSIE/gi.test(navigator.userAgent));
script.src = (is_ie) ? 'js/ieskins.js' : 'js/moreskins.js';
document.getElementsByTagName('head')[0].appendChild(script);
}());
</script>
Using navigator.userAgent is better and simple. You code doesn't work in any else browser except IE,because only IE understand the mean of <!--[if IE]>
<script type="text/javascript">
var ie = false;
if(/MSIE/gi.test(navigator.userAgent)){
ie = true;
}
if(ie == false)
{
document.write ("<script src="js/moreskins.js" type="text/javascript">")</script>;
} else {
document.write ("<script src="js/ieskins.js" type="text/javascript">")</script>;
}
</script>
I'm kinda new to JSP and right now I'm stumped on a problem that most of us wouldn't wish to encounter: Internet Explorer 6.0 support.
I've scoured over Google and found no answer. My question is, is there a way to load a different Javascript inside a JSP according to the browser version?
My algorithm would be:
Check Internet Explorer version.
IF IE6, load ie6.js
ELSE load normal.js.
So far, this is how I load my JS.
<script type="text/javascript" src="../js/normal.js"></script>
You can use conditinal comments to load different Javascript for Internet Explorer.
So, for your case:
<!--[if !IE 6]><!-->
<script type="text/javascript" src="normal.js" />
<!--<![endif]-->
<!--[if IE 6]>
<script type="text/javascript" src="ie6.js" />
<![endif]-->
You can do this by navigator object like this:
var ua = navigator.userAgent;
if(navigator.appName == 'Microsoft Internet Explorer'){
//check for version
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null){
version = parseFloat( RegExp.$1 );
}
if(version == 6.0){
//load specific js for IE 6.0
var file = document.createElement("script");
file.setAttribute("type", "text/javascript");
file.setAttribute("src", "../js/ie_6.js");
document.getElementsByTagName("head")[0].appendChild(file);
}
else{
//load normal.js for other IE versions.
var file = document.createElement("script");
file.setAttribute("type", "text/javascript");
file.setAttribute("src", "../js/normal.js");
document.getElementsByTagName("head")[0].appendChild(file);
}
}
If you want to do this on the server side, you would have to check User-Agent HTTP header. After this you could set some attribute to current request and later check that attribute in JSP page:
...
String browserString = httpServletRequest.getHeader("User-Agent");
String browserVersion = ...; // Some user agent parsing and version determining
httpServletRequest.setAttribute("browserVersion", browserVersion);
User agent examples can be found on internet, e.g. http://www.useragentstring.com/pages/Internet%20Explorer/.
<script language = "javascript">
if (navigator.appName == "Microsoft Internet Explorer") {
begin = navigator.userAgent.indexOf("MSIE ") +
"MSIE ".length;
if(navigator.userAgent.indexOf(";", begin) > 0) {
end = navigator.userAgent.indexOf(";", begin);
} else {
end = navigator.userAgent.indexOf(")", begin)
+ 2;
}
document.getElementById("targetDiv").innerHTML =
"You are using Internet Explorer " +
navigator.userAgent.substring(begin, end);
}
}
</script>
why when make a condition to the IE browser,there added an else part? what's the meaning and effect of it? thank you,
You shouldn't use Javascript to sniff out Internet Explorer. Welcome to HTML conditional comments:
FOR DETECTING IE:
<!--[if IE]>
<!-- Add IE script file here -->
<![endif]-->
FOR DETECTING ANYTHING EXCEPT IE:
<!--[if !IE]> -->
<!-- Add non-IE script file here -->
<!-- <![endif]-->
Javascript isn't a good solution for browser sniffing because you should only use it as a way to "progressively enhance" a website. If your website is going to break when someone doesn't have Javascript enabled, then that's not acceptable. Build your website and use Javascript to add the effects necessary to create a better experience.
This is especially true if you already have a native way of detecting for Internet Explorer. Why not use a method which will work in all browsers as opposed to a method that will work in browsers that only allow Javascript? You can never be so sure.
I'm writing JSP pages and using Tomcat, and it needs to work for IE 7 in addition to Firefox and Chrome (client needs).
In my program, I include both pieces of code. It works properly for non-IE browsers.
My problem is that CODE A does not work properly for IE, in that it treats it like a comment rather than a conditional comment that it should be reading. Any idea why this would happen and how to fix it?
<script type="text/javascript">
...
<!-- CODE A -->
<!--[if IE]>
url = "http://" + "..." + "&var=1";
<![endif]-->
<!-- CODE B -->
<!--[if !IE]> -->
url = "http://" + "..." + "&var=1";
<!-- <![endif]-->
...
</script>
Conditional HTML comments only work in HTML. JavaScript is not HTML. Rather use conditional JS comments in JS:
var IE = /*#cc_on!#*/false;
(only IE will interpret the ! which effectively makes it true)
Then you can use it as follows
if (IE) {
url = "http://" + "..." + "&var=1";
} else {
url = "http://" + "..." + "&var=1";
}
Feature detection should however be preferred in JS.