简体   繁体   中英

How to add javascript file dynamically in the head of html?

From below code I am trying to load javascript file TestJScript.js dynamically and after loading want to call javascript function LoadData() exist in that file. But I am getting error please check image.

Note: Error get only on IE-8.0.6001 update 0.

Please suggest me correction such that It will work from 6 to all version of IE. Or any another solution.

if it require any windows updates. Please let me know.

Please don't suggest with JQUERY code

Javascript file code :

function LoadData() {   
    alert('ok');
}

Code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
        <script>

            function LoadJSFile() {

                    var js = document.createElement("script")
                    js.setAttribute("type", "text/javascript")
                    js.setAttribute("src", "C:\\TestJScript.js")                    
                    document.getElementsByTagName("head")[0].appendChild(js)

                    //call below function exist in TestJScript.js file
                    LoadData();
                }              

        </script>
      </head>
<body onload="LoadJSFile();">

</body>
</html>

Error Image: 在此处输入图片说明

Try this http://dustindiaz.com/scriptjs . Like this:

$script('yui-base.js', function() {
  // do stuff with base...
  $script(['yui-anim.js', 'yui-connect.js'], function() {
    // do stuff with anim and connect...
  });
  $script('yui-drag.js', function() {
    // do stuff with drag...
  });
});

The error reports a problem in the javascript file that you're loading. So the problem lies not in how you dynamically load the javascript file, but in the javascript file itself.

It looks like there is a problem with the file once it has loaded. Are you sure there is no syntax error in the file itself.

Also, I would recommend you use a relative path to the javascript file instead of the absolute path.

EDIT:

Try this:

function LoadJSFile() {
  var script = document.createElement('script');
  script.src = "C:\\TestJScript.js";
  script.onload = function () {
    LoadData();
  };

   document.getElementsByTagName("head")[0].appendChild(script)     
}

You could try the following:

<script>
function LoadJSFile(src, callback) {
    var js = document.createElement('script');
    js.src = src;
    js.async = true;
    js.onreadystatechange = js.onload = function() {
        var state = js.readyState;
        if (!callback.done && (!state || /loaded|complete/.test(state))) {
            callback.done = true;
            callback();
        }
    };
    document.getElementsByTagName('head')[0].appendChild(js);
}
LoadJSFile('C:\\TestJScript.js', function() { 
    LoadData();
});
</script>

If you are using c# code then another solution to solve this script error is, invoke script through c# code. Code:

/

/Assiging html value to control
                webBrowser.DocumentText = "HTML content";               
//Calling document load completed event
                webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;

void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {                
            HtmlDocument htmlDocument = webBrowser.Document;
            HtmlElement htmlElementHead = htmlDocument.GetElementsByTagName("head")[0];
            HtmlElement HtmlElementScript = htmlDocument.CreateElement("script");
            HtmlElementScript.SetAttribute("text", "C:\\TestJScript.js");
            htmlElementHead.AppendChild(HtmlElementScript);
            htmlDocument.InvokeScript("LoadData");
            webBrowser.DocumentCompleted -= webBrowser_DocumentCompleted;
        }

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