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>
Related
I am using JQuery Singature and I encountered this Error Message for above IE version:
Message: Canvas element does not support 2d context. jSignature cannot proceed.
update : After Added the (;) in the flashcanvas.js
The Problem is the Same:
Message: Expected ';'
flashcanvas.swf
Code: 0
I have downloaded the flashcanvas.swf and flashcanvas.js from:
https://github.com/brinley/jSignature/blob/master/libs/flashcanvas.js
https://github.com/brinley/jSignature/blob/master/libs/flashcanvas.swf
these two files I placed them in a Folder called Script.
It does not matter if I include or did not include flashcanvas.swf What I need to do?
here the javascript
<!--[if lte IE 9]>
<script type="text/javascript" src="Script/flashcanvas.js"></script>
<script type="text/javascript" src="Script/flashcanvas.swf"></script>
<![endif]-->
<script type="text/javascript" src="Script/jSignature.js"></script>
<script type="text/javascript">
$(document).ready(function (){
$("#divSignature").jSignature({width:400, height:140, mousedown:function(){}});
});
</script>
Just a syntax error, simply update the following at line 928 of flashcanvas.js:
function getSwfUrl(window) {
return ( (window[FLASH_CANVAS + "Options"] || {})["swfPath"] || BASE_URL ) + "flashcanvas.swf"; //Added semicolon
}
IE tend to be less lenient towards Javascript errors, which can be a good thing for programmers learning the language. Browsers like Chrome can accommodate a fair amount of JS errors and still correctly execute a 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/.
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.
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/
The below piece of code was working in IE6 & IE7 and almost all versions of FF. It just don't work in IE8. It doesn't work in the sense once I added the script tag in to HTML->HEAD element I don't see the script being loaded in the browser(the alerts in the script doesn't show up). I see the tags have been inserted in the HTML-HEAD though.
var head = document getElementsByTagName('head')[0];
// Check if the script is already loaded.
if (head ){
var script = document.createElement('script');
script.type = 'text/javascript';
script.language = 'JavaScript';
script.src = '/Tolven/scripts/' + jsFileName;
head.appendChild(script);
}
Does anybody have this issue? Or any clues to resolve this?
If this script is in <head> tag than head does not exists when this script is parsed and executed. So, of cource if (head) is false.
Your are using JS framework -- so feel free to use it's tools. And also do not forget to include Your framework, before using it.
<!-- if your are using mootools -->
<script type="text/javascript" src="mootools.js"></script>
<script type="text/javascript">
window.addEvent('domready', function() {
// Your code...
});
</script>
<!-- if your are using prototype -->
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
document.observe("dom:loaded", function() {
// Your code...
});
</script>
Consider using a library like RequireJS or LABjs that do the job of including scripts at runtime really well.
var head = document getElementsByTagName('head')[0];
should be
var head = document.getElementsByTagName('head')[0];
Script seems to work after this modification.
This is was actually working. There is was an error(it only happens in IE8) in one of the scripts that's inserted at runtime. Eventually it's not executing alerts in the pages loaded next. Thanks for your answers though.