代码之家  ›  专栏  ›  技术社区  ›  leora Matt Lacey

为什么找不到这个javascript函数?

  •  2
  • leora Matt Lacey  · 技术社区  · 14 年前

    我有一些应该在document.ready上运行的javascript代码,但是我在Firebug中不断得到一个错误,说找不到onload()方法。

    这是代码(它清楚地显示了那里的方法):

    <script type="text/javascript">
    
        var tl;
        function onLoad1() {
    
            debugger;
    
            var eventSource = new Timeline.DefaultEventSource(0);
    
            // Example of changing the theme from the defaults
            // The default theme is defined in 
            // http://simile-widgets.googlecode.com/svn/timeline/tags/latest/src/webapp/api/scripts/themes.js
            var theme = Timeline.ClassicTheme.create();
            theme.event.bubble.width = 350;
            theme.event.bubble.height = 300;
    
            var d = Timeline.DateTime.parseGregorianDateTime("1900")
            var bandInfos = [
                Timeline.createBandInfo({
                    width: "80%",
                    intervalUnit: Timeline.DateTime.DECADE,
                    intervalPixels: 200,
                    eventSource: eventSource,
                    date: d,
                    theme: theme,
                    layout: 'original'  // original, overview, detailed
                }),
                Timeline.createBandInfo({
                    width: "20%",
                    intervalUnit: Timeline.DateTime.CENTURY,
                    intervalPixels: 200,
                    eventSource: eventSource,
                    date: d,
                    theme: theme,
                    layout: 'overview'  // original, overview, detailed
                })
            ];
            bandInfos[1].syncWith = 0;
            bandInfos[1].highlight = true;
    
            debugger;
    
            tl = Timeline.create(document.getElementById("tl"), bandInfos, Timeline.HORIZONTAL);
            // Adding the date to the url stops browser caching of data during testing or if
            // the data source is a dynamic query...
            var jsonFile = "<%= Url.Content("~/scripts/Views/Business/")%>test.js?"+ (new Date().getTime());
    
            tl.loadJSON(jsonFile), function(json, url) {
                eventSource.loadJSON(json, url);
            });
        }
        var resizeTimerID = null;
        function onResize() {
            if (resizeTimerID == null) {
                resizeTimerID = window.setTimeout(function() {
                    resizeTimerID = null;
                    tl.layout();
                }, 500);
            }
        }
    </script>
    
    <script type="text/javascript">
    
        $(document).ready(function() {
            debugger;
            onLoad1();
        });
        </script>
    

    知道为什么它不能“找到”那个方法吗?

    编辑

    • 我把所有的javascript放在上面(删除了图片)
    • 我把它重命名为onload1(),但我仍然有同样的问题
    • 我将第一个代码移到了函数的下面,但仍然有相同的问题。
    3 回复  |  直到 14 年前
        1
  •  2
  •   Christian C. Salvadó    14 年前

    这里有语法错误:

    tl.loadJSON(jsonFile), function(json, url) {
                        ^ the parentheses is prematurely closed
        eventSource.loadJSON(json, url); 
    });
     ^ syntax error, unexpected token
    

    您必须删除后面的右括号 jsonFile 论点:

    tl.loadJSON(jsonFile, function(json, url) {
        eventSource.loadJSON(json, url);
    });
    

    这个语法错误破坏了函数声明,原因是什么 onLoad 未定义。

        2
  •  0
  •   Itay Moav -Malimovka    14 年前

    打开错误控制台(firefox的本机控制台,而不是firebug),您将看到原因。

        3
  •  0
  •   Joseph Yaduvanshi    14 年前

    我怀疑你的问题是引号不匹配,更改:

    var jsonFile = "<%= Url.Content("~/scripts/Views/Business/")%>test.js?"+ (new Date().getTime());
    

    到:

    var jsonFile = '<%= Url.Content("~/scripts/Views/Business/")%>' + "test.js?"+ (new Date().getTime());
    

    您应该在更改前逐字看到这行(`<%=%>标记)