简体   繁体   English

使用另一个JavaScript文件中的javascript文件中声明的全局变量

[英]Use global variables declared in a javascript file in another javascript file

I'm trying to get TestService.Server.WWW_SERVER_URL, but TestService.Server is undefined. 我正在尝试获取TestService.Server.WWW_SERVER_URL,但是TestService.Server未定义。 When I call test1(), it works well. 当我调用test1()时,它运行良好。 But I cannot access the object literal TestServer. 但是我无法访问对象文字TestServer。 Is there a different way? 有其他方法吗?

test.html test.html

<script type="text/javascript" language="javascript" src="TestService.js"></script>
<script type="text/javascript" language="javascript">
    function test() {
        alert("TestService.Server.WWW_SERVER_URL[" + TestService.Server.WWW_SERVER_URL + "]");
        //test1();
    }
</script>

TestService.js TestService.js

document.write("<scr" + "ipt type='text/javascript' src='TestServer.js'><" + "/scr" + "ipt>");

var TestService = {
    Server: TestServer,
    Delimiter: ""
};

function test1() {
    test2();
}

TestServer.js TestServer.js

var TestServer = {
    WWW_SERVER_URL: "http://www.test.com"
};


function test2() {
    alert("test2 has been called!");
}

You have this in your TestService.js 您在TestService.js中有这个

document.write("<scr" + "ipt type='text/javascript' src='TestServer.js'><" + "/scr" + "ipt>");

var TestService = {
    Server: TestServer,
    Delimiter: ""
};

you are trying to set a property in TestService with TestServer which hasnt loaded yet as you do not give time for the newly added script to load 您正在尝试使用尚未加载的TestServerTestService设置一个属性,因为您没有为新添加的脚本加载时间

TestService.Server will evaluate to undefined since TestServer does not exist yet 由于TestServer不存在,因此TestService.Server评估结果为undefined

Setup an onload function that will add your script and then set your TestService.Server variable when its loaded 设置一个onload函数,该函数将添加您的脚本,然后在加载时设置TestService.Server变量

var TestService = {
    Server: null,
    Delimiter: ""
};

function test1() {
    test2();
}

window.onload = function() {
    var head = document.querySelector("head");
    var script = document.createElement("script"); 
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", "TestServer.js");

    head.addEventListener("load", function(event) {
        if (event.target.nodeName === "SCRIPT"){
            TestService.Server = TestServer;
        }
    }, true);

    head.appendChild(script); 
}

If you attach scripts dynamically, IE, Firefox, and Chrome will all download the scripts in an asynchronous manner. 如果您动态附加脚本,则IE,Firefox和Chrome都将以异步方式下载脚本。

Firefox and Chrome will wait till all of the async requests return and then will execute the scripts in the order that they are attached in the DOM but IE executes the scripts in the order that they are returned over the wire. Firefox和Chrome将等待所有异步请求返回,然后按照脚本在DOM中附加的顺序执行脚本,而IE则按照通过有线返回的顺序执行脚本。

source 资源

In your case, you can't gurantee TestServer.js get executed before TestService.js . 就您而言,您无法保证TestServer.jsTestService.js之前执行。 So I will recommend you change the way you access global variable cross-file. 因此,我建议您更改访问全局变量交叉文件的方式。

You can add TestServer.js to your html right before TestService.js , so they can execute one by one. 您可以添加TestServer.js之前到您的HTML TestService.js ,这样他们就可以执行一个接一个。

Anyhow, it is NOT recommended to do stuff like this, you can wrap them in your own namespace. 无论如何,不​​建议这样做,您可以将它们包装在自己的名称空间中。 Plus you'd better check the variable you want to use cross-file whether it's undefined before you use it. 另外,您最好在使用前检查要使用交叉文件的变量是否未定义。

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

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