[removed] inside javascript code [removed]()

前端 未结 5 828
忘掉有多难
忘掉有多难 2020-12-31 17:53

I got a portion of javascript code embedded in HTML (generated on the server side) that looks like this:

function winWriteMail2(){
  var win = open(\'\',\'wi         


        
5条回答
  •  抹茶落季
    2020-12-31 18:48

    Think your DOM-based code is fine, but try to (a) use the absolute script URL, (b) set the script type and (c) update src after appending, this should make it working more reliably:

    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.type = "text/javascript";
    head.appendChild(script);
    script.src = "http://host.tld/js/JSFILE.js";
    

    Hope this helps.

    EDIT

    By the way, it is good idea to set up kind of callback, to make sure script was loaded before using its code. Code can look similarly to this:

    // most browsers
    script.onload = callback;
    // IE
    script.onreadystatechange = function() {
       if(this.readyState == "loaded"  || this.readyState == "complete") {
          callback();
       }
    }
    

    Here callback is literally the function to execute.

提交回复
热议问题