简体   繁体   English

从另一个引用一个Java脚本库

[英]Referencing one java script library from another

I'm just getting started defining and implementing external javaScript libraries and I'm a little confused by the rules. 我才刚刚开始定义和实现外部javaScript库,我对规则有些困惑。 Below is the contents of three files "top.js", "bottom.js", and "ref.html". 以下是三个文件“ top.js”,“ bottom.js”和“ ref.html”的内容。 The file "bottom.js" contains a reference to "top.js" and the file "ref.html" contains a reference to "bottom.js". 文件“ bottom.js”包含对“ top.js”的引用,文件“ ref.html”包含对“ bottom.js”的引用。 In "ref.html" I have attempted to access functions in "top.js" by directly calling function and by calling the function via another function in "bottom.js", neither approach seems to be working. 在“ ref.html”中,我试图通过直接调用函数并通过“ bottom.js”中的另一个函数来调用该函数来访问“ top.js”中的函数,但两种方法似乎都不起作用。 Any suggestions would be appreciated. 任何建议,将不胜感激。

topTest.js: topTest.js:

function top_test() {
alert('Test from top');
}

bottom.js 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 ref.html

<html>
<head>
<script type="text/javascript" src="bottom.js"></script>
</head>
<body>
test
<script type="text/javascript">
bottom();
top();
</script>
</body>
</html>

The <script> tags have to be removed from the .js files. <script>标记必须从.js文件中删除。

These tags are only needed inside a HTML document, used to mark a part of the content as a script. 这些标记在HTML文档中需要,用于将内容的一部分标记为脚本。 The whole content of a JavaScript file is JavaScript, so adding the <script> tags do not make any sense, and is therefore invalid. JavaScript文件的整个内容都是JavaScript,因此添加<script>标记没有任何意义,因此是无效的。

To include a JavaScript from within a script in the <head> , you can use a library, or one the following method: 要在<head>的脚本中包含JavaScript,可以使用库或以下方法之一:

Create a <script> tag using document.createElement , and insert the script in the document. 使用document.createElement创建<script>标记,并将脚本插入文档中。 Example of your bottom.js : 您的bottom.js

function bottom() {
    alert("bottom");
    top();
}
loadScript('top.js');    // Call function (function declarations are evaluated
                         //   before the rest of the code, so this works)

function loadScript(file_name) {
    if (document.readyState === 'loading') { // Chrome
        document.write('<script src="' + file_name.replace(/"/g, '&quot;') + '"></script>');
        return;
    }
    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);
}

Dont use html tags in .js files. 不要在.js文件中使用html标记。 Just plain javascript codes 只是普通的JavaScript代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM