2011-04-09 79 views
0

我将函数从html页面移动到了包含的参考文件。它停止工作。将JavaScript函数移动到外部.js文件时停止工作

文件的完整内容是:

alert('file included'); 

function doAlert() { 
    alert('functions work'); 
} 

在HTML页面中,我有

<html> 
<head><title>Page</title></head> 
<body> 
    HTML Template Header 

    Some Content 

    ASP.NET MVC Partial View Starts 
    <script type="text/javascript"> 
     doAlert(); 
    </script> 
    ASP.NET MVC Partial View ends 

    HTML Template Footer 

    <script type="text/javascript" src="/Scripts/wtf.js"></script> 
</body> 
</html> 

“文件包括”警报的作品,但“职能的工作”没有。我究竟做错了什么?

回答

3

确保在执行该功能之前包含该文件。

<script src="file.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    doAlert(); 
</script> 
+0

啊,这显然是我必须做的不对 – smartcaveman 2011-04-09 23:59:04

1

,你应该打电话给你以下面的方式功能(S):

window.onload = function() { 
    doAlert(); 
    // func_1(); 
    // func_2(); 
    // func_3(); 
    // ... 
    // func_n(); 
}; 

有关在window.onload事件的更多信息,请参见this

Code example on jsFiddle

+0

是有办法订阅多种功能火的onload像'+ ='在C#中? – smartcaveman 2011-04-10 00:09:10

+0

@smartcaveman这很容易做到,你只需要将它们列入匿名函数:window.onload = function(){func1(); FUNC2(); FUNC3(); ... funcn(); }; – 2011-04-10 00:15:10

+0

如果您想在不同的时间注册它们,该怎么办? – smartcaveman 2011-04-10 00:17:41

相关问题