简体   繁体   中英

event.returnValue is not working 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="http://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;
            }           
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM