Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "Function expected" in IE : '<input onclick="start();" ... '

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>
  1. Original case: Failed (That's why I'm here.)
  2. Use a span instead of input: Success
  3. Use another function name: Success
  4. Use a different type: Failed
  5. Use onmouseover instead of onclick: 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.

like image 840
johnchen902 Avatar asked Jun 17 '13 13:06

johnchen902


2 Answers

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.

like image 59
apsillers Avatar answered Oct 23 '22 14:10

apsillers


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).

like image 36
Entity Black Avatar answered Oct 23 '22 15:10

Entity Black