我刚开始定义和实现外部的javaScript库,我对规则有点困惑。以下是三个文件“top.js”,“bottom.js”和“ref.html”的内容。文件“bottom.js”包含对“top.js”的引用,文件“ref.html”包含对“bottom.js”的引用。在“ref.html”中,我试图通过直接调用函数并通过“bottom.js”中的另一个函数调用函数来访问“top.js”中的函数,但这两种方法似乎都不起作用。任何建议,将不胜感激。从另一个引用一个java脚本库
topTest.js:
function top_test() {
alert('Test from top');
}
bottom.js
function bottom() {
alert("bottom");
top_test();
}
loadScript('topTest.js'); // Call function (function declarations are evaluated
// before the rest of the code, so this works)
function loadScript(file_name) {
var newScript = document.createElement('script');
var scripts = document.getElementsByTagName('script');
// Reference to the latest (this) <script> tag in the document
scripts = scripts[scripts.length-1];
// Set target
newScript.src = file_name;
// Clean-up code:
newScript.onload = newScript.onerror = function() {
this.parentNode.removeChild(this);
};
// Insert script in the document, to load it.
scripts.parentNode.insertBefore(newScript, scripts);
}
ref.html
<html>
<head>
<script type="text/javascript" src="bottom.js"></script>
</head>
<body>
test
<script type="text/javascript">
bottom();
top();
</script>
</body>
</html>
这是答案的一部分;你可能还想解释如何在另一个JS文件中“包含”一个JS文件。 ['*咳嗽*'](http://requirejs.org/) – 2012-03-21 20:50:55
谢谢您的回复。我已经删除了标签(在上面的代码中编辑过),但它仍然不起作用。 – 2012-03-21 20:53:57
@ user996035查看'bottom.js'内容的更新回答。 – 2012-03-21 20:57:57