javascript - Is e = event || window.event; acceptable for IE8 compatibility - Stack Overflow

This code (taken from this post) has stopped me getting this error message in IE8 when trying to use pr

This code (taken from this post) has stopped me getting this error message in IE8 when trying to use preventDefault():

Error: 'null' is null or not an object

$(document).ready(function () {
    $("#submitBtn").on("click", pD(event));
});

function pD(e) {
    e = event || window.event;
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

It seems to work. However the ment in this post (fourth ment down) has me concerned that maybe this is a bad method. Is it acceptable, or unacceptable in some way? The language of the ment is pretty strong, so it makes me wonder.

This code (taken from this post) has stopped me getting this error message in IE8 when trying to use preventDefault():

Error: 'null' is null or not an object

$(document).ready(function () {
    $("#submitBtn").on("click", pD(event));
});

function pD(e) {
    e = event || window.event;
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

It seems to work. However the ment in this post (fourth ment down) has me concerned that maybe this is a bad method. Is it acceptable, or unacceptable in some way? The language of the ment is pretty strong, so it makes me wonder.

Share Improve this question edited Mar 14, 2023 at 18:41 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 28, 2013 at 20:23 12527481252748 15.4k34 gold badges117 silver badges242 bronze badges 4
  • 4 should it not be e = e || window.event – karthikr Commented May 28, 2013 at 20:24
  • 1 There's no point having a function argument e if the first line of the function immediately overwrites it with another value. Use karthikr's suggestion so that the argument is used as-is if it has a (truthy) value. – nnnnnn Commented May 28, 2013 at 20:24
  • @nnnnnn okay yeah, typo. so now I get the error Error: Object required in the line if(e.preventDefault){. – 1252748 Commented May 28, 2013 at 20:25
  • 1 jQuery's event parameter is not a native event object. It's already fixed for you. So why are you trying to fix it again? – user1106925 Commented May 28, 2013 at 20:29
Add a ment  | 

1 Answer 1

Reset to default 10

Firstly the way you are assigning the click handler is incorrect:

$("#submitBtn").on("click", pD(event));

You are invoking the pD() function and passing its return value to the .on() method. You need to just pass a reference to the function:

$("#submitBtn").on("click", pD);

Secondly, you are using jQuery, so you don't need to test the e argument and set it to window.event because jQuery handles those patibility issues for you. You can use e.preventDefault() knowing jQuery will just make it happen.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745329705a4622820.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信