包含xml文件的javascript文件

问题描述 投票:1回答:2

我有这个代码......

    <script type="text/javascript">
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", "Client-Controls/ClientGridView.xml", true);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;

    var oHead = document.getElementsByTagName('HEAD').item(0);
    var x = xmlDoc.getElementsByTagName("script");
    for (i = 0; i < x.length; i++) {
        var oScript = document.createElement("script");
        oScript.language = "javascript";
        oScript.type = "text/javascript";
        oScript.src = x[i].childNodes[0].nodeValue;
        oHead.appendChild(oScript);
    }
</script>

这渲染一些,但我不能使用javascript文件的功能......有什么可以做的诀窍?

javascript xml
2个回答
0
投票

您的脚本将异步加载,因此您无法立即使用它们。给他们一个onload处理程序,以便你知道他们什么时候准备好了:

var oScript = document.createElement("script");
oScript.type = "text/javascript";
oScript.src = x[i].childNodes[0].nodeValue;
oScript.onload = function () {
    // run bar, which is contained in foo.js
    bar();
};
oHead.appendChild(oScript);

如果要确保所有脚本都已加载,可以创建一个计数器:

function counter(count, callback) {
    return function() {
        count -= 1;
        if (count === 0) {
            callback();
        }
    }
}

并将其传递给每个加载处理程序:

var x = xmlDoc.getElementsByTagName("script"),
    countdown = counter(x.length, function(){ alert("All scripts loaded!"; });
for (i = 0; i < x.length; i++) {
    var oScript = document.createElement("script");
    oScript.type = "text/javascript";
    oScript.src = x[i].childNodes[0].nodeValue;
    oScript.onload = countdown;
    oHead.appendChild(oScript);
}

0
投票

扩展Dennis的观点,在你想要获取的Javascript文件中添加一个变量并检查该变量是否存在或者是否为整数,检查该值。无论如何,您可以在Javascript中轻松完成此操作。这有两种方式。 方案一

var neededScript = document.createElement("script");
n//if you want the script to be asynchronous, then set the async attribute like this:
neededScript.async="";
//then point the [src] attribute to the required script.
neededScript.src="src";
document.head.appendChild(neededScript);
Optionally, if you want to append the script to the body, then instead of document.head.appendChild use document.body.appendChild. You can append children to any element using either document.getElementById or document.getElementsByClassName or if you want to filter elements by tag name (<html> is a tag) then use document.getElementByTagName or - my personal favourite coming from jQuery - document.querySelector. You should read up on all of these methods as they are crucial to manipulating the DOM.

Second Option

Second option is using a very popular library called jQuery. It's syntax is a lot shorter than the document API syntax. It's main purpose is making DOM manipulation easier.

var requiredScriptSrc="src";
$("head").append("<script src='"+requiredScriptSrc+"'></script>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
jQuery uses selectors, you should be familiar with them if you've tried CSS or document.querySelector.

Option Three

Option three is to use custom tags and a for loop that gets an attribute of the custom tag and creates a script element.

for(var i = 0; i<document.getElementsByTagName("js-module").length; i++){
  var s = document.getElementsByTagName("js-module")[i].getAttribute("src");
  var r = document.createElement("script");
  r.src = s;
  document.head.appendChild(r);
}
<js-module src="src"></js-module>

如果您需要更多帮助,请在此帖子上发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.