简体   繁体   English

如何在html头中动态添加javascript文件?

[英]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. 从下面的代码中,我试图动态加载JavaScript文件TestJScript.js,并且在加载后要调用该文件中存在的javascript函数LoadData()。 But I am getting error please check image. 但是我遇到错误,请检查图像。

Note: Error get only on IE-8.0.6001 update 0. 注意:错误仅在IE-8.0.6001更新0上获得。

Please suggest me correction such that It will work from 6 to all version of IE. 请建议我进行更正,以使其能够从6到IE的所有版本。 Or any another solution. 或任何其他解决方案。

if it require any windows updates. 如果需要任何Windows更新。 Please let me know. 请告诉我。

Please don't suggest with JQUERY code 请不要建议使用JQUERY代码

Javascript file code : Javascript文件代码:

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 . 试试这个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. 该错误报告您正在加载的javascript文件中的问题。 So the problem lies not in how you dynamically load the javascript file, but in the javascript file itself. 因此,问题不在于如何动态加载javascript文件,而在于javascript文件本身。

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. 另外,我建议您使用javascript文件的相对路径,而不要使用绝对路径。

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. 如果使用的是c#代码,则另一个解决此脚本错误的方法是通过c#代码调用脚本。 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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