加载js动态不反映

时间:2013-06-12 14:58:07

标签: javascript jquery html html5

我想动态加载js,但是之前加载了一个包含在索引中的js文件(我正在动态加载文件)是否有办法修复订单

我正在动态添加global.js,我想要在global.js中声明变量,然后在加载dynamicjs.DynamicJs之前初始化

------------- Index.html -------------

<!DOCTYPE HTML>
<html>
       <head>
              <meta http-equiv="X-UA-Compatible" content="IE=edge">
              <script src="resources/sap-ui-core.js"
                      id="sap-ui-bootstrap"
                      data-sap-ui-libs="sap.ui.commons"
                      data-sap-ui-theme="sap_goldreflection" >
              </script>
              <!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
              <script src="js/dyn.js"></script>
              <script></script>
              <script>
                     sap.ui.localResources("dynamicjs");
                     var view = sap.ui.view({id:"idDynamicJs1", viewName:"dynamicjs.DynamicJs", type:sap.ui.core.mvc.ViewType.JS});
                     view.placeAt("content");
              </script>
       </head>
       <body class="sapUiBody" role="application">
              <div id="content"></div>
       </body>
</html>

function loadScript(url){
    var xhrObj = createXMLHTTPObject();
    // open and send a synchronous request
    xhrObj.open('GET', url, false);
    xhrObj.send('');

    var e = document.getElementsByTagName("script")[1];
    var d = document.createElement('script');
    d.src = url;
    d.type = 'text/javascript';
    d.async = false;
    d.defer = false;
    e.parentNode.insertBefore(d,e);
}


function addScriptDynamically(){
    loadScript('js/global.js'+'?scheme=12345');
}

function createXMLHTTPObject(){
    var xmlhttp=new XMLHttpRequest();
    if( typeof xmlhttp == 'undefined' || xmlhttp == null )
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    return xmlhttp;
}

/*call function */
addScriptDynamically();

1 个答案:

答案 0 :(得分:0)

为什么不尝试使用jQuery.getScript()

加载脚本

http://api.jquery.com/jQuery.getScript/

如果您想要与原型或mootools等其他库兼容,您可以设置jQuery.noConflict()