正确与正确在另一个js中调用一个js文件的最简单方法 [英] Correct & simplest way of calling one js file inside another js
问题描述
在定义特定对象后,我想在代码的body标签中调用第三方js.在我的js文件中添加第三方js的最佳方法是什么?我读到下面是做到这一点的一种方法,但这是有害的.
I want to call a third party js in the body tag of my code after I define a particular object. Whats the best way to add the third party js inside my js file? I read that below is one way to do it but it is harmful.
var myObject = { /*this is my object */};
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "thirdparty.js";
document.body.appendChild(js);
我是js新手.我研究了&看到了诸如reuire js等的选项,但我不太清楚.有人可以建议我正确和正确吗?在我的js文件中包含第三方js的最简单方法?办法吗?我希望仅在我在主体中定义myObject之后才能调用此js. \
I'm a js newbie. I researched a bit & saw options such as reuire js etc but I'm not very clear. Can any one suggest me the correct & simplest way to include third party js in my js file? way to do it? I want this js to be called only after I define myObject in the body. \
像这样:@veritas
Like this: @veritas
myObject = { //my object };
add_html += ' < div id="xyz" >';
add_html += '< /div >';
$some_div.find('li').eq(2).after( add_html );
$('#xyz').append(myObject);
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "thirdparty.js";
document.body.appendChild(js);
推荐答案
如果要使用JS插入第三方库,则可以添加事件侦听器以确保已加载事件侦听器,然后运行代码.
If you want to insert the 3rd party library with JS, you can add an event listener to make sure it's loaded and then run your code.
var myObject = { /*this is my object */},
js = document.createElement("script");
js.type = "text/javascript";
js.onload = function () {
myObject.callMethod();
}
document.body.appendChild(js);
js.src = "thirdparty.js";
最重要的部分是在附加侦听器之后放置js.src =
设置,因为资源可能会触发load
,然后才设法向其添加事件侦听器.
The most important part is to put the js.src =
setting after attaching your listener because the resource may trigger load
before your manage to add a event listener to it.
编辑
我不清楚这部分做什么js.onload = function(){myObject.callMethod(); } ..什么是callMethod()?
I'm not clear what does this part do js.onload = function () { myObject.callMethod(); } .. What is callMethod()? .
.onload
部分
.onload
part
每个资源元素(例如script
)在成功将内容加载到文档内部后都会触发事件load
. .onload
部分正在向该特定事件添加事件侦听器(注意:此代码也可以用不同的方式编写,例如js.addEventListener("load", callback)
).
为什么要在load
之后触发代码?很简单,因为浏览器正在执行提取操作并异步执行,并且您期望从thirdparty.js
获取的对象或方法可能在脚本中的下一个操作之前尚未准备就绪(因此您将获得undefined
值).
Every resource element like script
will trigger an event load
after successfuly loading the content inside your document. The .onload
part is adding an event listener to that particular event (NOTE: this code can be also written differently eg. js.addEventListener("load", callback)
).
Why do we want to trigger the code after load
? Simple, because the browser is doing the fetch and execute asynchronously and the objects or methods you expect to get from the thirdparty.js
may not be ready before next operation in your script (so you would get undefined
values).
.callMethod()
部分
.callMethod()
part
这只是我的伪代码,我假设您想从myObject
运行一个使用某些thirdlibrary
对象和方法的方法.
This is just my dummy code, I was assuming you want to run a method from myObject
that uses some thirdlibrary
objects and methods`.
这篇关于正确与正确在另一个js中调用一个js文件的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!