gpt4 book ai didi

Javascript : How to get value of variable from specific file from 2 different file?

转载 作者:行者123 更新时间:2023-11-30 15:17:08 25 4
gpt4 key购买 nike

我有 1 个页面从另一台服务器加载 Js 文件。它为电子商务商店中的每个产品加载文件,其 id 在 url 中见以下结构。

产品 1: http://static.www.xxx.com/mydata/uXKojYEd9WXFpAasite/v4_3/1/d/itemjs

产品 2:

http://static.www.xxx.com/mydata/uXKojYEd9WXFpAasite/v4_3/2/d/itemjs

产品 3:

http://static.www.xxx.com/mydata/uXKojYEd9WXFpAasite/v4_3/3/d/itemjs

所有的Js文件都包含如下代码

var MyItemData={"counts":{"q":1,"a":1,"r":2,"ar":4,"rr":0,"dq":1,"da":1,"c":0,"sdsd":0},"active":true};

现在我正在像下面这样在 Html 中读取这些数据

var mycounta = MyItemData.counts.a;
var mycountq = MyItemData.counts.q;
var mycountr = MyItemData.counts.r;

这里的问题是我只能获取最后一个产品的数据,因为变量 MyItemData 对于所有文件都是相同的。我必须通过 id 读取每个文件,但我不知道实现它的正确方法。有没有人试过这样的事情?

最佳答案

您可以循环下面的答案,动态加载外部 js 文件。

How do I load a javascript file dynamically?

在每次迭代中,读取 MyItemData 并使用它。

通过在上面的链接中使用函数loadJS

<script type="application/javascript">
var listOfJSFiles=["http://static.www.xxx.com/mydata/uXKojYEd9WXFpAasite/v4_3/1/d/itemjs", "http://static.www.xxx.com/mydata/uXKojYEd9WXFpAasite/v4_3/2/d/itemjs", "http://static.www.xxx.com/mydata/uXKojYEd9WXFpAasite/v4_3/3/d/itemjs"];
var listOfMyItemData=[];

function loadJS(file) {
// DOM: Create the script element
var jsElm = document.createElement("script");
// set the type attribute
jsElm.type = "application/javascript";
// make the script element load file
jsElm.src = file;
// finally insert the element to the body element in order to load the script
document.body.appendChild(jsElm);
}

var arrayLength = listOfJSFiles.length;
for (var i = 0; i < arrayLength; i++) {
loadJS(listOfJSFiles[i]);
listOfMyItemData.push(MyItemData);
//removing listOfJSFiles[i] from html is recommended.
}

</script>

关于Javascript : How to get value of variable from specific file from 2 different file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44301156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com