0

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:enter image description here

Sam
  • 433
  • 1
  • 10
  • 26

5 Answers5

0

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...
  });
});
Maysam
  • 7,246
  • 13
  • 68
  • 106
0

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.

marty
  • 651
  • 7
  • 13
0

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)     
}
Sam
  • 433
  • 1
  • 10
  • 26
Husman
  • 6,819
  • 9
  • 29
  • 47
  • Husman, It works on all IE version except IE-8. I have added script file code also. – Sam Jun 23 '14 at 13:37
0

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>
chridam
  • 100,957
  • 23
  • 236
  • 235
0

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;
        }
Sam
  • 433
  • 1
  • 10
  • 26