给javascript加入include(import)命令

来源:百度文库 编辑:神马文学网 时间:2024/05/16 23:35:23
大多数语言都有包含其它代码文件的命令,如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\">");
} else {
document.write("<" + "script src=\"" + base + path + "\">");
}
}
附带几个类型判断的函数:
//*********************************************************
// 判断类型
//*********************************************************
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‘;
}

_xyz