One of our customers reported that some ASP.NET Ajax controls generate on the page links of the form
<a href="javascript:doSomething()">Click</a>
This could be a problem due to an
issue on Internet Explorer.
A simple workaround to this issue could be the following code that changes the <a> tags in order to place the javascript call on the onclick event:
[syntax="JS"] function reparseLinks(container) {
//please ignore (and delete) the <b></b> code inside this string as it is
//added by the forum parser...sorry
var jsstr = "javascript:";
var links = container.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
if (links
.href.indexOf(jsstr) == 0) {
var command = links.href.substring(jsstr.length) +";";
links.href = "#";
var closure = function(com) {
return function() {
eval(com);
return false;
}
}(command);
if (links.attachEvent) {
links.attachEvent("onclick", closure);
} else if (links.addEventListener) {
links.addEventListener("click", closure, false);
}
}
}
}[/syntax]
Note that this code is only needed on the "attachEvent" case so you can easily modify it if you don't want it to run on "addEventListener" browsers (e.g. Firefox).
You can also "improve performance" extracting the anonymous function that creates the closure.
You can use this method on the entire document
[syntax="JS"]reparseLinks(document);[/syntax]
or just on the tag that contains the links to be modified
[syntax="JS"]reparseLinks(document.getElementById("linkContainer"));[/syntax]