简体   繁体   中英

How to append script tag dynamically in asp.net user control?

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');
        }
}

将新创建的标记添加到文档中:

document.getElementsByTagName('head')[0].appendChild(scriptGoogle);
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "url to the script file here");
document.getElementsByTagName("head")[0].appendChild(script);

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 (eg 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:

window.setTimeout(function()
{
    // object/variable "newScriptObject" is defined in the new script
    if(!newScript)
    {
        // script timed out; let's proceed with plan B
    }
    else
    {
        // script ready, proceed as planned;
        // although, like I said, I think it's more precise if you execute this code in your myscript.js instead - as maybe that code will be ready before the 5 sec assumed here
        // alternatively, use window.setInterval to check for objects defined this script every so milliseconds, in which case, when you find the script, don't forget to stop that timer (window.clearInterval)
    }
}, 5000); // wait 5 sec for the new script

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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