• 在HTML中动态加载Javascript文件<AJAX的方法>好像还有点问题


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title></title>
    </head>
    <body>
        <input type="text" id="txt1" value="xxxxx" />
        <input type="text" id="txt2" value="yyyyy" />
       <script language="javascript" type="text/javascript">
       <!--
           function GetHttpRequest() {
               var xmlhttp_request = false;
               try {
                   if (window.ActiveXObject) {
                       for (var i = 5; i; i--) {
                           try {
                               if (i == 2) {
                                   xmlhttp_request = new ActiveXObject("Microsoft.XMLHTTP");
                               }
                               else {
                                   xmlhttp_request = new ActiveXObject("Msxml2.XMLHTTP." + i + ".0");
                               }
                               break;
                           }
                           catch (e) {
                               xmlhttp_request = false;
                           }
                       }
                   }
                   else if (window.XMLHttpRequest) {
                       xmlhttp_request = new XMLHttpRequest();
                       if (xmlhttp_request.overrideMimeType) {
                           xmlhttp_request.overrideMimeType("text/xml");
                       }
                   }
               }
               catch (e) {
                   xmlhttp_request = false;
               }
               return xmlhttp_request;
           }
           function AjaxPage(sld, url) {
               var oXmlHttp = GetHttpRequest();
               if (oXmlHttp) {
                   oXmlHttp.OnReadyStateChange = function() {
                       if (oXmlHttp.readyState == 4) {
                           if (oXmlHttp.status == 200 || oXmlHttpRequest == 304) {
                               IncludeJS(sld, url, oXmlHttp.responseText);
                           }
                           else {
                               alert("XML request error:" + oXmlHttp.statusText + "(" + oXmlHttp.status + ")");
                           }
                       }
                   };
                   oXmlHttp.open("GET", url, true);
                   oXmlHttp.setRequestHeader("Content-Type", "text/xml");
                   oXmlHttp.setRequestHeader("Content-Type", "gb2312");
                   oXmlHttp.send(null);
               }
           }
           function IncludeJS(sld, fileUrl, source) {
               if ((source != null) && (!document.getElementById(sld))) {
                   var oHead = document.getElementsByTagName("HEAD").item(0);
                   var oScript = document.createElement('script');

                   oScript.language = "javascript";
                   oScript.type = "text/javascript";
                   oScript.id = sld;
                   oScript.defer = true;
                   oScript.text = source;

                   oHead.appendChild(oScript);
               }
           }

           AjaxPage("scrA", "JS/b.js");
           alert("主页面动态加载JS脚本");
           alert("主页面动态加载b.js并取其中的变量:" + str);
       //-->
        </script>
    </body>
    </html>

    附:其它的三种方法

    动态加载js文件的方法(缺陷:不能使用js中的全局变量,如果是在html>body之后的代码中动态加载的话,可能还有其它的缺陷):

    1.直接document.write
    <script language="javascript"> document.write("<script src='test.js'><\/script");
    </script>

    2.动态改变已经存在的script的src
    <script src='' id='s1'></script>
    <script language="javascript">
      s1.src='test.js';
    </script>

    3.动态创建script元素
    <script>
      var oHead = document.getElementsByTagName('HEAD').item(0);
      var oScript = document.createElement('script');
      oScript.type = 'text/javascript';
      oScript.src = 'test.js';
      oHead.appendChild(oScript);
    </script>

    此上三种方法都是异步执行的,无法保证加载后脚本具体执行的情况,因为IE无法被我们的脚本所阻塞,无法要求加载过程中断,等待我们取得必要的信息。在加载脚本时,主页面的脚本在继续运行,如果用以上的方法,那如下代码:

    要动态加载的JS脚本:a.js,以下是该文件内容:
    var str = "中国";
    alert("这是a.js中的变量:"+str);

    对话框将不能弹出,报错:'str'未定义

  • 相关阅读:
    js倒计时的实现
    用Math获取随机数的方法抽奖
    计算器的实现
    放大镜
    关于轮播图,我知道的不多。
    jqery标签页
    jQuery鼠标划入划出
    说说手机页面
    简单说说tab标签页和轮播图
    前端中的那些小事
  • 原文地址:https://www.cnblogs.com/Denny_Yang/p/1961531.html
Copyright © 2020-2023  润新知