简体   繁体   中英

Object doesn't support this property or method - IE 7/8

Any idea why this won't work in IE 7/8? (works fine in IE 9 and 10, FF and Chrome)

When I click the "Send" button, the console shows :

SCRIPT438: Object doesn't support this property or method 
script.1383689376.js, line 94 character 3
(Line 94) : token = $("#token").val();

HTML:

<div class="comment_heading">Leave a Comment</div>
<div class="post_comment">
<textarea name="txtpostcomment" id="txtpostcomment-'.$postid.'" class="txtpostcomment"></textarea>
<button class="btnpostcomment" id="btnpostcomment-'.$postid.'" onclick="comment('.$postid.');" type="button">Send</button>
<input type="hidden" name="token" id="token" value="'.$_SESSION['token'].'">
<script>document.getElementById("txtpostcomment-'.$postid.'").focus();</script>
</div>

Script:

comment = function(postid1)
{
    txt =  $('#txtpostcomment-'+postid1);
    btn =  $('#btnpostcomment-'+postid1);

    comment1 = $(txt).val();
    token = $("#token").val();

    $(btn).css('background-image', 'url(/comments/submit-busy.gif)');
    $(btn).attr('disabled', true);
    $(btn).attr('disabled', true);
    ....
    ....
}

This line of HTML:

<input type="hidden" name="token" id="token" value="'.$_SESSION['token'].'">

creates a property of the global object named "token" that is a reference to the input element.

In this line:

  token = $("#token").val();

you have an undeclared identifier. When this line is executed, IE tries to create a global variable token (because of the undeclared variable) but because there already is one (the aforementioned DOM element), IE throws an error.

Why IE doesn't simply assign the new value is a question that's been asked for over a decade, you won't get a sensible answer.

The simple fix is to declare all variables .

It is particularly bad to use a function expression to assign to an undeclared variable. It has zero benefits over a function declaration and has some serious drawbacks (you've just discovered one). So always declare variables in an appropriate scope and always use function declarations unless you have a very good reason to use a function expression.

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