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>
- Original case: Failed (That's why I'm here.)
- Use a
span
instead ofinput
: Success - Use another function name: Success
- Use a different type: Failed
- Use
onmouseover
instead ofonclick
: Failed
I 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.
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>
- Original case: Failed (That's why I'm here.)
- Use a
span
instead ofinput
: Success - Use another function name: Success
- Use a different type: Failed
- Use
onmouseover
instead ofonclick
: Failed
I 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.
Share Improve this question edited Jun 17, 2013 at 14:20 johnchen902 asked Jun 17, 2013 at 13:57 johnchen902johnchen902 9,6091 gold badge29 silver badges70 bronze badges 5- 2 first things first, remove all javascript from your html! keep your javascript in a js file. :) – Johan Commented Jun 17, 2013 at 14:00
- 2 @Johan Actually I did, and I got the error, then I put JavaScript in HTML to post here. – johnchen902 Commented Jun 17, 2013 at 14:02
-
Probably you've redefined another
start
variable with some other type than function. It can also be anid
of an HTML element shadowing the original global variable. – Teemu Commented Jun 17, 2013 at 14:02 - @Teemu There are no such things in the code I given, nor in the code I am developing. – johnchen902 Commented Jun 17, 2013 at 14:03
-
1
Copying your code into a fiddle and checking on IE 10 seems to reproduce the problem, but it can be fixed by just changing the name of the
start
function to something else. Also, it's better to bind your event handler in JS rather than inline them anyway. – Matt Burland Commented Jun 17, 2013 at 14:29
2 Answers
Reset to default 12start
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 autoplete(){
alert("");
}
function vspace(){
alert("");
}
function hspace(){
alert("");
}
</script>
<input id="error" onclick="autoplete();" 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).