我有一些应该在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>
任何想法为什么它不能“找到”那种方法。
答案 0 :(得分:2)
这里有语法错误:
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);
});
这个SyntaxError打破了函数声明,这就是为什么没有定义onLoad
的原因。
答案 1 :(得分:0)
打开错误控制台(Firefox的本机版本,而不是firebug),你会看到原因。
答案 2 :(得分:0)
我怀疑你的问题是不匹配的引号,更改:
var jsonFile = "<%= Url.Content("~/scripts/Views/Business/")%>test.js?"+ (new Date().getTime());
到:
var jsonFile = '<%= Url.Content("~/scripts/Views/Business/")%>' + "test.js?"+ (new Date().getTime());
您应该在更改之前逐字地看到此行(`&lt;%=%&gt;标记)