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.
Related
Im developing Windows 10 store apps Javascript/Html and since there is Microsoft EDGE in apps as the browser, inline scripting no longer works. If i put the code in an external file, the page loads, but none of the click events work. Is there any solution for this. Small example where onclick attribute does not work.
Code
default.html 7 default.js
// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
function gored() {
document.body.style.backgroundColor = red;
}
(function () {
"use strict";
WinJS.Binding.optimizeBindingReferences = true;
var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
var isFromBackground = false;
app.onactivated = function (args) {
var localSettings = Windows.Storage.ApplicationData.current.localSettings;
if (args.detail.kind === activation.ActivationKind.launch) {
if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
// TODO: This application has been newly launched. Initialize
// your application here.
} else {
// TODO: This application has been reactivated from suspension.
// Restore application state here.
}
args.setPromise(WinJS.UI.processAll());
}
};
app.oncheckpoint = function (args) {
// TODO: This application is about to be suspended. Save any state
// that needs to persist across suspensions here. You might use the
// WinJS.Application.sessionState object, which is automatically
// saved and restored across suspension. If you need to complete an
// asynchronous operation before your application is suspended, call
// args.setPromise().
isFromBackground = true;
};
app.start();
})();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>App1</title>
<!-- WinJS references -->
<!-- To get the latest version of WinJS, go to: http://go.microsoft.com/fwlink/?LinkId=533245 -->
<link href="WinJS/css/ui-dark.css" rel="stylesheet" />
<script src="WinJS/js/WinJS.js"></script>
<!-- App1 references -->
<link href="/css/default.css" rel="stylesheet" />
<script src="/js/default.js"></script>
</head>
<body>
<p>Content goes here</p>
<button onclick="gored()"> Click Me</button>
</body>
</html>
I went into detail about this in a blog post.
Windows HTML5 apps have a strict security setting, especially when it comes to injecting code at runtime via JavaScript. I ran into this issue before as well.
You can wrap the function that you are using to inject the Javascript with another function, MSApp.execUnsafeLocalFunction().
When attempting to dynamically insert a div, Windows 8 throws an error. Specifically, it’s when trying to use something like:
div.innerHTML = "A string of some stuff"
HTML1701: Unable to add dynamic content ' a' A script attempted to inject dynamic content, or elements previously modified dynamically, that might be unsafe. For example, using the innerHTML property to add script or malformed HTML will generate this exception. Use the toStaticHTML method to filter dynamic content, or explicitly create elements and attributes with a method such as createElement. For more information, see http://go.microsoft.com/fwlink/?LinkID=
Reason:
The reasoning behind all of these problems is the same, so I’ll just state it here once for the sake of brevity. `Microsoft fears that the string can be intercepted somewhere along the line, and malicious content can be added to the values of your string.
Work Around:
The big issue with this method is that you’re trying to use innerHtml. Instead, use .append.
That still won’t work if you just try to pass in a string, however. What you need to do is set your string to a variable, then pass in that variable. If you do not create an object (that is, setting the string to a variable) then this will not work. If you just try to use a string, then you’ll see nothing but text where the div should be.
Here’s a single line example:
$panel.append('<'img src="' + item.thumbImageUrl +'" >');
If you try to pass that in, Windows 8 will throw the error seen above. Even if I wrap that in MSApp.execUnsafeLocalFunction() I will still see an error.
The workaround is as follow:
var appendString = '<'img src="' + item.thumbImageUrl '" >';
$panel.append(appendString);
Because I’m now taking that string and setting it to a variable (thereby turning it into an object), Windows 8 will allow me to pass in that object and create dynamic content.
Even then, it will occasionally throw the error above. HOWEVER, if you were to wrap that object in MSApp.execUnsafeLocalFunction(), you would then be in the clear. WinJS offers a function to wrap your own functions in, which allows you to basically say “I take responsibility for this function, and I assure you it’s safe.” That function is called: MSApp.execUnsafeLocalFunction().
So the final solution looks like this:
var appendString = '<'img src="' + item.thumbImageUrl '" >';
MSApp.execUnsafeLocalFunction(function() {
$panel.append(appendString);
});
You can read more about this issue here.
Further Reading:
execUnsafeLocalFunction from MSDN
TutsPlus tutorial on jQuery and Win8
I had a similar problem and found that a script read in the header did not work but when I moved it to the body, it did:
WORKS ON MOST BUT NOT ALL PAGES WITH 'EDGE'. WORKS WITH ALL PAGES ON ALL OTHER BROWSERS:
LT script type="text/javascript" src="../ie5.js" GT LT /script GT
LT script type="text/javascript" src="../common_functions.js" GT LT /script GT
LT /head GT
LT body GT
WORKS ON ALL PAGES WITH 'EDGE' AND OTHER BROWSERS:
LT /head GT
LT body GT
LT script type="text/javascript" src="../ie5.js" GT LT /script GT
LT script type="text/javascript" src="../common_functions.js" GT LT /script GT
Why? Only Microsoft will know.
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));
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.
<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.
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/