简体   繁体   English

如何在另一个JavaScript文件的功能内使用远程存储的JavaScript文件中的变量?

[英]How could I use the variables from a remotely stored JavaScript file within the function of another JavaScript file?

Currently, I'm trying to take an array from a JavaScript file stored on a web page (ex. "website.com/javascript.js") and run it through a function stored in a JavaScript file I have made for a website. 目前,我正在尝试从存储在网页上的JavaScript文件(例如“ website.com/javascript.js”)中提取数组,并通过存储在为网站制作的JavaScript文件中的函数运行它。 This is for a game. 这是一个游戏。

Previously, I was attempting to import both .js files into the HTML file and use the array from the website as an input for my own JavaScript file, but I couldn't seem to do this. 以前,我曾尝试将两个.js文件导入HTML文件,并将网站中的数组用作我自己的JavaScript文件的输入,但我似乎无法做到这一点。

I simply couldn't find the answer online (or didn't know how to phrase the question right). 我只是无法在线找到答案(或者不知道如何正确表达问题)。 If I need to import anything for the purpose of doing this, could you tell me what to import? 如果需要为此目的导入任何东西,您能告诉我要导入什么吗? Thank you! 谢谢!

Here's the code I'm working on. 这是我正在处理的代码。 Sorry for the very novice code, I haven't worked much in JavaScript. 对非常新手的代码感到抱歉,我在JavaScript中工作不多。

function makeBracket(players){
    var resultString;
    for (var i = 0; i < players.length; i++){
       var assassinName = players[i][1];
       var targetCode = players[i][4];
       for(var j=0;j<players.length;j++){
            var code = players[j][2];
            if(code == targetCode){
                var targetName = players[j][1];
                document.write(assassinName + " -> " + targetName + "<br>");
       }
    }
}

I'll be using the function.js for the file that contains your function, and array.js/array.json for the file that contains the data you need. 我将对包含您的函数的文件使用function.js ,对包含所需数据的文件使用array.js/array.json

Using a global variable 使用全局变量

You could include both files, and expose the array on the first file as a global variable. 您可以同时包含两个文件,并将第一个文件上的数组公开为全局变量。 To include them, use two script tags on your HTML, in order: 要包括它们,请在HTML上依次使用两个script标签:

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

array.js : array.js

window.myArray = [/* Data here */];

function.js : function.js

var array = window.myArray;
doStuff(array);

Loading via AJAX 通过AJAX加载

An alternative is to store the array in JSON format, and load it asynchronously using AJAX. 另一种选择是将数组存储为JSON格式,然后使用AJAX异步加载。

Consider the following function.js : 考虑以下function.js

$.getJSON("/array.json", function(array) {
  doStuff(array);
});

When loaded (I'm assuming jQuery here, since it is in the tags of the question), it will make a request to /array.json , parse it as JSON, and pass the results to the callback function. 加载后(我假设这里是jQuery,因为它位于问题的标记中),它将向/array.json发出请求,将其解析为JSON,并将结果传递给回调函数。 From there, you can do anything with the data. 从那里,您可以对数据进行任何处理。

An advantage of this method is that you don't need to worry about ordering the files anymore, and you don't use any global variables. 这种方法的优点是您无需担心文件的顺序,也无需使用任何全局变量。 However, it is slightly more complex to understand, and there'll be a small delay between the page loading and the data arriving. 但是,它的理解要稍微复杂一点,并且页面加载和数据到达之间会有一个小的延迟。

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

相关问题 如何使用JavaScript中另一个文件中的变量和函数? - How do I use variables and function from another file in javascript? 如何使用另一个Javascript文件中的函数? - How can I use a function from one Javascript file in another? 如何使用另一个JavaScript文件中的参数创建一个JavaScript函数以在另一个JavaScript中使用? - how to create a javascript function with parameters in another javascript file to use within another javascript? 从另一个 javascript 文件读取/使用变量 - Read/Use variables from another javascript file 如何使用JavaScript中另一个函数的变量 - How can I use variables from another function in javascript 如何使用另一个PHP文件中的变量将其包含在HTML页面javascript函数中 - How to use variables from another php file to include in a html page javascript function 如何在另一个 javascript 文件中的 javascript 文件中使用 javascript 常量 - How to use javascript constant in javascript file from another javascript file 如何使用 jQuery 从另一个文件调用 JavaScript function - How to use jQuery to call a JavaScript function from another file 你如何使用另一个文件中的 JavaScript function - How do you use a JavaScript function from another file 如何在我在HTML文件中编写的脚本中访问JavaScript文件中的变量? - How can I access variables from a JavaScript file from within a script I wrote in an HTML file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM