只需在函数范围之外的global.js中定义变量:
// global.jsvar global1 = "I'm a global!";var global2 = "So am I!";// other js-filefunction testGlobal () { alert(global1);}为确保此方法有效,您必须先包含/链接到global.js,然后再尝试访问该文件中定义的任何变量:
<html> <head> <!-- Include global.js first --> <script src="/YOUR_PATH/global.js" type="text/javascript"></script> <!-- Now we can reference variables, objects, functions etc. defined in global.js --> <script src="/YOUR_PATH/otherJsFile.js" type="text/javascript"></script> </head> [...]</html>
当然,如果您不想让js文件的加载中断初始页面加载,则可以在结束
-tag之前链接脚本标签。


