动态加载外部资源文件

功能描述:根据当前页面的状态(读/写)来加载不同的外部资源(js、css等),从而达到合理加载页面内容,提高页面响应速度,优化应用性能的目的。

实例参考:

 

<script language="JavaScript">
  var bIsNewDoc="<Computed Value>"
  var strUnid = "<Computed Value>"
  var bIsEdited = "<Computed Value>";
  var strWfStatus= "<Computed Value>";
  var  hWinDlg = null;
  if (window.execScript == null) {
    window.execScript = function(script) {
      eval(script);
    }
  }
  function useScript (name) {
    try {
  var loader = xmlhttp();
  loader.open('GET', name, false);
  loader.send(null);
  window.execScript(loader.responseText.toString());
 } catch (e) {
  alert(e);
 }
  }
  function transferJs(name) {
    try {
      useScript(name);
    }catch (e) {
      alert(e);
    }
  }
 
  function xmlhttp() {
    try{return new ActiveXObject("Msxml2.XMLHTTP");} catch(e){}
    try{return new ActiveXObject("Microsoft.XMLHTTP");} catch(e){}
    try{return new XMLHttpRequest();} catch(e){}
    alert("XMLHttpRequest Object not existed!!");
    return null;
  }
/*Customize when doc status is read or edit.
========================Start======================================================*/
  if (bIsNewDoc || (bIsEdited&&strWfStatus=="0")) {
    var externalFiles= [
      {Url: "/DFCS/dfcs.nsf/Resource/css/Common.css",  Type:"css"},   //表单样式
      {Url: "/DFCS/dfcs.nsf/FormDisposal.js",   Type:"javascript"},  //表单布局
      {Url: "/DFCS/dfcs.nsf/selPerson.js",    Type:"javascript"},  //选人控件
      {Url: "/DFCS/dfcs.nsf/Validate.js",    Type:"javascript"},  //元素校验
      {Url: "/DFCS/dfcs.nsf/MetaObject.js",      Type:"javascript"},  //元数据
      {Url: "<Computed Value>",         Type:"javascript"}  //日历控件kludge:目前必须放在最后
    ];
  }
  else{
    var externalFiles= [
      {Url: "/DFCS/dfcs.nsf/Resource/css/Common.css",  Type:"css"},   //表单样式
      {Url: "/DFCS/dfcs.nsf/FormDisposal.js",   Type:"javascript"},  //表单布局
      {Url: "/DFCS/dfcs.nsf/MetaObject.js",      Type:"javascript"}  //元数据
    ];
  }
//=================================End=================================================
  intFilesTot = externalFiles.length;
  var oHead = document.getElementsByTagName('HEAD').item(0);        
  for(var i=0;i<intFilesTot;i++){  
    var sType = externalFiles[i].Type;
    if(sType=="javascript"){
 transferJs(externalFiles[i].Url);
    }
    else{
      if(sType=="css"){
        var cssTag = document.createElement('link');
    cssTag.setAttribute("rel","stylesheet");
    cssTag.setAttribute("type","text/css");
    cssTag.setAttribute("href",externalFiles[i].Url);
    oHead.appendChild(cssTag);
      }       
    }
  }
</script>

posted @ 2010-04-08 18:57  风影极光  阅读(364)  评论(0编辑  收藏  举报