简体   繁体   English

脚本标记中的onload事件未触发

[英]onload event in script tag not firing

To preface, I have looked at the existing posts about this issue on SO and I believe my code incorporates all suggested fixes, yet it still doesn't work. 作为序言,我查看了关于SO的有关此问题的现有文章,我相信我的代码包含了所有建议的修复程序,但仍然无法正常工作。

I am trying to get a simple onload event to fire: 我正在尝试触发一个简单的onload事件:

var test = function() {
    console.log("Script loaded! V2")
}

//Append new user script to head 
const userProg = document.createElement('script')
userProg.type = 'text/javascript'
userProg.id = 'user-program'

userProg.addEventListener('load', test, false)
userProg.onload = function() {
    console.log("Script loaded! V1")
}

userProg.text = [goatPuzzleSetupCode, genericSetupCode, userCode, resultToJSONCode].join('\n')
document.head.appendChild(userProg)

What's frustrating is that I have gotten the onload event to fire before, but I don't have a copy of my code from when it was working. 令人沮丧的是,我之前已经触发过onload事件,但是没有工作时的代码副本。 So I'm not sure what the issue is. 所以我不确定是什么问题。

It sounds like you're looking for an event like afterscriptexecute , which is unfortunately non-standard and shouldn't be used. 听起来您正在寻找类似afterscriptexecute之类的事件,不幸的是这是非标准的,不应该使用。 But, because you're inserting the script text directly already, it should be simple enough to add something else to the text that fires a window function, assuming the appended script doesn't contain errors. 但是,由于您已经直接插入了脚本文本,因此假设添加的脚本中没有错误,应该足够简单地向触发window功能的文本中添加其他内容。 For example: 例如:

 window.scriptLoaded = function() { console.log("Script loaded! V2") } const text1 = 'console.log("script1 running")'; const text2 = 'console.log("script2 running")'; //Append new user script to head const userProg = document.createElement('script') userProg.text = [text1, text2, 'scriptLoaded();'].join('\\n'); document.head.appendChild(userProg) 

( onload doesn't work because, as a comment said: onload无效,因为如评论所述:

The onload is for when the element loads. onload用于元素加载时。 i,e finished downloading. 我完成下载。

but there's nothing for a script without a src to download) 但是没有src的脚本就什么也没有下载)

Onload is not working in your case because there is nothing to load for an inline script It will work for the scripts that will be downloaded ie remote scripts . Onload在您的情况下不起作用,因为内联脚本没有要加载的内容 ,它将对将要下载的 remote scriptsremote scripts起作用。

However also need to take care about 1 thing for remote scripts : 但是,还需要注意remote scripts 1件事:

Need to set the src attribute after the onload event. 需要在onload事件之后设置src属性。

 //Append new user script to head var goatPuzzleSetupCode= console.log("Script goatPuzzleSetupCode"); var genericSetupCode= console.log("Script genericSetupCode"); var userCode= console.log("Script userCode"); var resultToJSONCode= console.log("Script resultToJSONCode"); var userProg = document.createElement('script'); var script="//cdnjs.cloudflare.com/ajax/libs/less.js/1.3.3/less.min.js"; //userProg.addEventListener('load', test, false); userProg.onload = function(script) { console.log("Script loaded! 1"+script) test(); }; userProg.src=script; userProg.text = [goatPuzzleSetupCode, genericSetupCode, userCode, resultToJSONCode].join('\\n') document.head.appendChild(userProg); var test = function() { console.log("Script loaded! 2") } 

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

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