【发布时间】:2017-02-08 22:39:39
【问题描述】:
如何修改以下代码?在从函数 yourCodeToBeCalled 添加下一个代码之前,我想准确加载 两个 脚本。有人可以给初学者一个提示吗?我在评论后给出了我的建议//。
var loadJS = function(url, implementationCode){//url, url_Next
//url is URL of external file, implementationCode is the code
//to be called from the file, location is the location to
//insert the <script> element
var scriptTag = document.createElement('script');//will add scriptTag_Next
scriptTag.type = 'text/javascript';
scriptTag.setAttribute('nonce','22os9h3sdfa');
scriptTag.src = url;
scriptTag.onload = implementationCode;//Will It be work? -> scriptTag.onload = scriptTag_Next = implementationCode;
scriptTag.onreadystatechange = implementationCode;//like as above scriptTag_Next.onreadystatechange ??
document.body.appendChild(scriptTag);
};
var yourCodeToBeCalled = function(){
console.log('okay'); } loadJS('js/jquery-3_1_1.min.js', yourCodeToBeCalled);//My my suggestion loadJS('js/jquery-3_1_1.min.js', 'js/test.js' yourCodeToBeCalled)
我借用的原始代码:link
【问题讨论】:
-
“我想在同一个 js 文件中添加下一个代码之前恰好加载两个脚本” - 你能重新表述一下你的说法吗?
-
为什么有人会降低我的主题评分?我尝试了很多组合以获得正确的结果。在下一个 js 代码之前是否可能加载多个脚本?
-
不确定我是否理解,但为什么不加载脚本两次(通过创建 2 个脚本标签并填充)然后将代码添加到另一个 JS 文件中并以相同的方式加载它,这样你最终会加载 3 个脚本动态。
-
再次提醒:如果您需要等待脚本,不要为该脚本使用
async。async属性适用于当您明确不想希望等待它完成加载后再继续下一步的情况。 -
什么都没有减轻:您看到页面更快,但总的零到所有数据传输解析和活动是完全相同的,所以您为用户只看到比以前更早的页面支付的价格是(a)您的用户在“真正完成”之前与您的页面进行交互(可能有一些,甚至很多缺少功能)和(b)您不再保证任何事件顺序,因此如果您想“在运行脚本 3 之前等待 2 个脚本”,使用
async完成会破坏任何执行此操作的能力。
标签: javascript html