te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Error "Function expected" in IE : '<input onclick="start();" ...
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

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.

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 an id 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
Add a ment  | 

2 Answers 2

Reset to default 12

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

发布评论

评论列表(0)

  1. 暂无评论