Rising Sun

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  148 随笔 :: 0 文章 :: 22 评论 :: 0 Trackbacks

大多数语言都有包含其它代码文件的命令,如ASP和c/c++下的Include,java下的import,唯独javascript好像没这功能,为了应付工作,特写了如下这个函数:

//******************************************************
// 包含文件 用法: $import('../include/mian.js', 'js');
//                 $import('../style/style.css', 'css');
//******************************************************
function $import(path, type){
 var i,
      base,
      src = "common.js",
      scripts = document.getElementsByTagName("script");

 

 for (i = 0; i < scripts.length; i++) {
      if (scripts[i].src.match(src)) {
          base = scripts[i].src.replace(src, "");
          break;
      }
  }
 
  if (type == "css") {
      document.write("<" + "link href=\"" + base + path + "\" rel=\"stylesheet\" type=\"text/css\"></" + "link>");
  } else {
      document.write("<" + "script src=\"" + base + path + "\"></" + "script>");
  }
}

附带几个类型判断的函数:

//******************************************************
// 判断类型
//******************************************************
function isAlien(a) {
  return isObject(a) && typeof a.constructor != 'function';
}

 

function isArray(a) {
  return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
  return typeof a == 'boolean';
}

function isEmpty(o) {
  var i, v;
  if (isObject(o)) {
    for (i in o) {
      v = o[i];
      if (isUndefined(v) && isFunction(v)) {
        return false;
      }
    }
  }
  return true;
}

function isFunction(a) {
  return typeof a == 'function';
}

function isNull(a) {
  return typeof a == 'object' && !a;
}

function isNumber(a) {
  return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
  return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
  return typeof a == 'string';
}

function isUndefined(a) {
  return typeof a == 'undefined';
}

posted on 2009-07-23 13:50 brock 阅读(165) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: