2012-05-22 148 views
1

我在我单独的js文件中调用了一个函数hello()。访问js中的函数

在我的网页我有这个js:

<script type='text/javascript'>//do hello fnc</script> 

我怎样才能获得在页面脚本中调用我的主要JS功能文件?

+0

页面上什么..? – Alnitak

回答

3

在之前的其他脚本标记的src属性中称之为,其中涉及其在此脚本中的任何功能。

<script type='text/javascript' src='main.js'></script> 
<script type='text/javascript'> 
    hello(); 
    // Anything defined in previously included js files like main.js 
    // is now available, as well as anything defined on this page. 
</script> 
+1

作为旁注'type ='text/javascript''不再是必要的 – fcalderan

0

首先加载外部文件并调用函数:

<script src=external.js></script> 
<script> 
    hello(); 
</script> 
0

你只需要包括外部js文件:

<script type="text/javascript" src="MyOtherFile.js"></script> 

然后只要在你的代码要呼叫功能:

<script type="text/javascript">hello();</script>