event.returnValue in IE6
The following code should not open a new window in IE and Firefox. Its not opening in Firefox, but it is opening in IE. Whats going wrong?
var EventLib = {
"preventDefault" : function(event){
if(event.preventDefault) {
event.preventDefault();
}else{
window.event.returnValue = false;
}
}
}
window.onload = function(){
var elem = document.getElementById("link");
elem.onclick = function(e){
EventLib.preventDefault(e);
}
}
and the HTML is:
<a id="link" href="开发者_如何学Pythonhttp://www.google.com" target="_blank">Click</a>
It could be that evaluating the expression event.preventDefault
throws an error when event
is undefined. Try using if (event && event.preventDefault)
rather than just if (event.preventDefault)
.
Just change function as I have shown it below, it will work
var EventLib = {
"preventDefault" : function(event){
if(!event)
event = window.event;
if(event.preventDefault) {
event.preventDefault();
}else{
window.event.returnValue = false;
}
}
}
精彩评论