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

    function loadJS(id, url){
    var xmlHttp = null;
    if(window.ActiveXObject)
    {
    try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    } else if(window.XMLHttpRequest) //Firefox,Opera 8.0+,Safari,Chrome
    {
    xmlHttp = new XMLHttpRequest();
    }
    //采用同步加载
    xmlHttp.open("GET", url, false);
    //发送同步请求,如果浏览器为Chrome或Opera,必须发布后才能运行,不然会报错
    xmlHttp.send(null);
    //4代表数据发送完毕
    if(xmlHttp.readyState == 4)
    {
    //0为访问的本地,200到300代表访问服务器成功,304代表没做修改访问的是缓存
    if((xmlHttp.status >= 200 && xmlHttp.status < 300) || xmlHttp.status == 0 || xmlHttp.status == 304)
    {
    var myHead = document.getElementsByTagName("head").item(0)
    var myScript = document.createElement("script")
    myScript.language = "javascript";
    myScript.type = "text/javascript";
    myScript.id = id;
    try {
    //IE8以及以下不支持这种方式,需要通过text属性来设置
    myScript.appendChild(document.createTextNode(xmlHttp.responseText));
    } catch(ex) {
    myScript.text = xmlHttp.responseText;
    }
    myHead.appendChild(myScript);
    return true;
    } else
    {
    return false;
    }
    } else
    {
    return false;
    }
    }

  • 相关阅读:
    CF676E:The Last Fight Between Human and AI
    BZOJ2079: [Poi2010]Guilds
    BZOJ4518: [Sdoi2016]征途
    BZOJ2216: [Poi2011]Lightning Conductor
    51nod1766 树上的最远点对
    洛谷P1257 平面上的最接近点对
    BZOJ2144: 跳跳棋
    BZOJ4773: 负环
    BZOJ4552: [Tjoi2016&Heoi2016]排序
    The Falling Leaves(建树方法)
  • 原文地址:https://www.cnblogs.com/zhengyan/p/6197513.html
Copyright © 2011-2022 走看看