zoukankan      html  css  js  c++  java
  • 动态加载js

    function GetHttpRequest() {
                if (window.XMLHttpRequest) // Gecko 
                    return new XMLHttpRequest();
                else if (window.ActiveXObject) // IE 
                    return new ActiveXObject("MsXml2.XmlHttp");
            }
     
            function AjaxPage(url,callBack) {
                var oXmlHttp = GetHttpRequest();
     
                oXmlHttp.onreadystatechange = function() {
                    if (oXmlHttp.readyState == 4) {
                        if (oXmlHttp.status == 200 || oXmlHttp.status == 304) {
                            document.getElementById("div_msg").innerHTML = document.getElementById("div_msg").innerHTML + ('得到js文本<br>');
                            createScript(oXmlHttp.responseText);
                            document.getElementById("div_msg").innerHTML = document.getElementById("div_msg").innerHTML + ('3秒后使用新的js<br>');
                            window.setTimeout(function() { callBack.call(); }, 3000);
                        }
                        else {
                            alert('XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')');
                        }
                    }
                }
                oXmlHttp.open('GET', url, true);
                oXmlHttp.send(null);
    //            oXmlHttp.open('POST', "handler1.ashx", true); 
    //            oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  //ajax post请求比较给头信息加上这句。否则后台context.Request.Form访问不到
    //            oXmlHttp.send("name=22222222222222");
            }
     
            function createScript(source) {
                if ((source != null)) {
                    var oHead = document.getElementsByTagName('HEAD').item(0);
                    var oScript = document.createElement("script");
                    oScript.language = "javascript";
                    oScript.type = "text/javascript";
                    oScript.defer = true;
                    oScript.text = source;
                    oHead.appendChild(oScript);
                }
     
            }
     
            window.onload = function() {
                document.getElementById("div_msg").innerHTML = document.getElementById("div_msg").innerHTML + ('现在开始加载js<br>');
                AjaxPage("ganttchart/base_jjl.js", function() {
                    document.getElementById("div_msg").innerHTML = document.getElementById("div_msg").innerHTML + ('完毕<br>');
                    MyAlert("test");
                });
                
                
            } 
  • 相关阅读:
    Linux设备驱动之Ioctl控制
    虚拟内存与物理内存的区别
    怎么远程控制他人电脑
    二维数组和指针
    二维数组和指针(C语言)
    帧率、码流与分辨率相关知识
    深入理解FIFO
    安装lsb_release
    Linux初学之vmware Workstation 网络连接三种模式
    RTSP协议学习笔记
  • 原文地址:https://www.cnblogs.com/jianjialin/p/1772990.html
Copyright © 2011-2022 走看看