如何在asp.net用户控件中动态附加脚本标签?
我有用户控件(myUserControl.ascx)。因为我试图附加脚本标签并动态分配“src”,然后检查 onload 和 onerror 事件,但它似乎不起作用。我在按钮单击时调用 PerformDynamicJS。
function PerformDynamicJS(){
var scriptGoogle = document.createElement("script");
scriptGoogle.type = "text/javascript";
scriptGoogle.src = "myscript.js";
scriptGoogle.onload = function (evt) {
alert('Inside onload');
}
scriptGoogle.onerror = function (evt) {
alert('Inside on error');
}
}
I have user control (myUserControl.ascx). in that i am trying to append script tag and assigning "src" dynamically and then checking onload and onerror events, but it seem to be not working.I am calling PerformDynamicJS on button click.
function PerformDynamicJS(){
var scriptGoogle = document.createElement("script");
scriptGoogle.type = "text/javascript";
scriptGoogle.src = "myscript.js";
scriptGoogle.onload = function (evt) {
alert('Inside onload');
}
scriptGoogle.onerror = function (evt) {
alert('Inside on error');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将新创建的标签添加到文档中:
Add your newly created tag to the document:
我之前用脚本查找过“onload”,我不记得找到过这样的事件。我最终编写了一个基于计时器的 js 代码来频繁检查预期在正在下载的脚本中定义的对象。因此,要么您这样做,要么:为什么不简单地不在插入脚本标记的代码中执行任何操作 - 相反,当您的脚本准备就绪时,您有一个完美的时机:将“未包装”代码添加到脚本本身(例如
alert("I,脚本已加载,我已准备好使用我提供的这些功能");
。并且,这里没有“错误”情况:脚本将下载或者它不会(无论出于何种原因:如果确实[下载],则在执行/使用该脚本时可能发生的任何错误都与传输无关(这些错误的处理方式应与处理任何错误的方式相同)。其他脚本错误),所以你在这里使用“onerror”的意图不适合+1:)
编辑:如果它没有下载,那么它就不会执行,你将无法使用它。 。如果您的客户端代码确实在等待,那么您将不得不编写某种基于计时器的超时逻辑;例如,在上面的“add to head”行之后,执行如下操作:
最后,在此上下文中不存在“部分下载”之类的事情。正常的浏览器不会开始执行脚本不仅没有被完全下载,而且还没有被解析(解释)。 “解释”部分就是为什么当您在某处缺少大括号或由于其他语法错误时,您会看到 JS 错误,这些错误通常指向不相关的位置。至少缺少一个大括号就属于这一类,因为这种语法错误实际上完全“转移”(某种)代码块,使得无法弄清楚什么是什么。抱歉在这里有点偏离主题。
I looked for "onload" with scripts before, I don't remember finding such event. I ended up writing a timer-based js code to frequently check for an object that is expected to be defined in the script that is being downloaded. So, it's either that you do that, or: why not simply not do anything from within the code that inserted the script tag - instead, you have a perfectly timed moment when your script is ready: add "unwrapped" code to the script itself (e.g.
alert("I, the script, loaded and I'm ready with these functions that I provide");
.And, there is no "on error" case here: the script will either download or it will not (for whatever reason: unavailability, connectivity, server-error, etc). If it does [download], any errors that may be occurring while that script is being executed/used are not really transport-related (those errors should be treated the same way you treat any other script error), so your intention with "onerror" here isn't a fit (IMO). +1 :)
EDIT: If it doesn't download, then it will not execute, and you will not be able to use it. If your client code is really waiting, then you'll have to write some kind of timer-based timeout logic; for example, after the "add to head" line above, do something like this:
Finally, there is no such thing as "partial download" in this context. A sane browser will not start executing a script that hasn't been fully, not only downloaded, but also parsed (interpreted). The "interpreted" part is why you'll see JS errors that often point to unrelated location when you miss a brace somewhere or due to other syntax errors. Missing a brace, at least, is in this category, as such syntax error practically "shifts" (kind of) code blocks completely, making it impossible to figure out what's what. Sorry for going a little bit off-topic here.