There is a piece of HTML:
<input onclick="start();" type="button" value="Start!"/>
In Firefox and Chrome it invokes start
on click. However in IE9 it produces the error message: SCRIPT5002: Function expected
. Then I tried several different cases:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function start(){
alert("Let's start");
}
function stop(){
alert("Let's stop");
}
</script>
<input onclick="start();" type="button" value="Start!"/><br/> <!-- Failed : the main issue -->
<span onclick="start();">Start!</span><br/> <!-- Work -->
<input onclick="stop();" type="button" value="Stop!"/><br/> <!-- Work -->
<input onclick="start();" type="text" value="Start!"/><br/> <!-- Failed -->
<input onmouseover="start();" type="button" value="Start!"/><br/> <!-- Failed -->
</body>
</html>
span
instead of input
: Successonmouseover
instead of onclick
: FailedI used debugger and it said start
is function start(){alert("Let's start");}
. Then I used alert(start)
and it said fileopen
.
As the result of test 3, I can easily work-around by changing the name, but I want to know why this error would happened in some cases but not happened in the others. Edit: Or the next best thing, other work-arounds without changing the names.
start
is a property of input
elements in IE. (According to Microsoft, it indicates, "when a video clip file should begin playing." I have no idea what that means, but it's not important.) In your console, you can see this by running:
> document.createElement("input").start
"fileopen"
In an element's inline event handler, all the properties of that element are available as top-level variables. It's as if the inline code is run inside a with(thisParticularElement)
block (where thisParticularElement
is the element with the listener, obviously). Therefore, in the context of the inline event code, the variable start
refers to the start
property of the element, which is a string, not a function.
Simply change the function name, or use window.start
explicitly.
this is interesting. I did few test on my own and it seems that this error happen because start is already property of input element. So it will try to call property, not the function.
I consider it as a bug.
Try following code:
<script>
function autocomplete(){
alert("");
}
function vspace(){
alert("");
}
function hspace(){
alert("");
}
</script>
<input id="error" onclick="autocomplete();" type="button" value="Start!"/>
<input id="error2" onclick="vspace();" type="button" value="Start!"/>
<input id="error3" onclick="hspace();" type="button" value="Start!"/>
Then try to debug in console. Also try to var x = document.getElementById("error");
and then debug x properties.
I tested this in IE8, IE9 and IE10. These functions doesnt work in my IE9 and IE10 (Win 7 32bit).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With