当前位置:编程学习 > JS >>

用ajax动态加载需要的js文件

答案:习惯了用java,在java中,需要某个类直接import就可以了,所以做javascript的时候也想实现这个效果。
  前段时间,用了下dojo,里面的dojo.require的功能很不错,一看代码,晕了,一大堆东西,唉~还是自己写个简单点的,dojo.require可以引入包,我就只做一个导入js文件的。
  开始用的document.write,执行顺序不对了,这是在后面进行导入,而我们总是在前面执行中就需要导入的js,这样,执行中就会报“某某未定义”,就是顺序问题了。
  接着,我就想用ajax同步(注意:不是异步)调用远程js文件,这里有个问题,就是我们要js文件的时候,不是用绝对路径,就是相对路径,用相对路径的话,以哪个为参照呢?可以用调用js的那个文件为参照,也可以以实现调用功能的js文件为参照,这里,我选择写个 js,实现按需调用其它js,参照也选它。经过一番修改,路径问题解决。但是,读取过来的数据中文会有乱码问题,好在我们做东西都用UTF-8(因为要国际化),所以这里避过了。
  远程js内容取到后,就要执行,用eval执行后,发现还是取不到远程js里定义的内容,怪了,猛alert一番后,发现执行eval的上下文范围有问题,我们要的是js在window对象中执行,嗯?window有什么方法没?一找,哈,有个window.execScript方法,ok,试一下,成功,yeah~后来发现在firefox下,不能用window.execScript,找了一下,用window.eval,用法和ie下的window.execScript类似。但是只用window.eval的时候,在ie下有时候会出问题,所以就两种一起用了。
  下面是实现远程js安调用的那个js:env.js,我已经习惯用oo写js了
复制代码 代码如下:

/**
* @author zxub 2006-06-01
* 状态信息显示类,用var Status=new function()定义,可以静态引用其中的方法
* 一般情况下为function Status(),这样不能静态引用其中的方法,需要通过对象来引用
*/
var Status=new function()
{
this.statusDiv=null;

/**
* 初始化状态显示层
*/
this.init=function()
{
if (this.statusDiv!=null)
{
return;
}
var body = document.getElementsByTagName("body")[0];
var div = document.createElement("div");
div.style.position = "absolute";
div.style.top = "50%";
div.style.left = "50%";
div.style.width = "280px";
div.style.margin = "-50px 0 0 -100px";
div.style.padding = "15px";
div.style.backgroundColor = "#353555";
div.style.border = "1px solid #CFCFFF";
div.style.color = "#CFCFFF";
div.style.fontSize = "14px";
div.style.textAlign = "center";
div.id = "status";
body.appendChild(div);
div.style.display="none";
this.statusDiv=document.getElementById("status");
}

/**
* 设置状态信息
* @param _message:要显示的信息
*/
this.showInfo=function(_message)
{
if (this.statusDiv==null)
{
this.init();
}
this.setStatusShow(true);
this.statusDiv.innerHTML = _message;
}

/**
* 设置状态层是否显示
* @param _show:boolean值,true为显示,false为不显示
*/
this.setStatusShow=function(_show)
{
if (this.statusDiv==null)
{
this.init();
}
if (_show)
{
this.statusDiv.style.display="";
}
else
{
this.statusDiv.innerHTML="";
this.statusDiv.style.display="none";
}
}
}

/**
* @author zxub
* 用于存放通道名称及通信对象的类,这样可以通过不同通道名称来区分不同的通信对象
*/
function HttpRequestObject()
{
this.chunnel=null;
this.instance=null;
}

/**
* @author zxub
* 通信处理类,可以静态引用其中的方法
*/
var Request=new function()
{
this.showStatus=true;

//通信类的缓存
this.httpRequestCache=new Array();

/**
* 创建新的通信对象
* @return 一个新的通信对象
*/
this.createInstance=function()
{
var instance=null;
if (window.XMLHttpRequest)
{
//mozilla
instance=new XMLHttpRequest();
//有些版本的Mozilla浏览器处理服务器返回的未包含XML mime-type头部信息的内容时会出错。因此,要确保返回的内容包含text/xml信息
if (instance.overrideMimeType)
{
instance.overrideMimeType="text/xml";
}
}
else if (window.ActiveXObject)
{
//IE
var MSXML = ['MSXML2.XMLHTTP.5.0', 'Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for(var i = 0; i < MSXML.length; i++)
{
try
{
instance = new ActiveXObject(MSXML[i]);
break;
}
catch(e)
{
}
}
}
return instance;
}

/**
* 获取一个通信对象
* 若没指定通道名称,则默认通道名为"default"
* 若缓存中不存在需要的通信类,则创建一个,同时放入通信类缓存中
* @param _chunnel:通道名称,若不存在此参数,则默认为"default"
* @return 一个通信对象,其存放于通信类缓存中
*/
this.getInstance=function(_chunnel)
{
var instance=null;
var object=null;
if (_chunnel==undefined)//没指定通道名称
{
_chunnel="default";
}
var getOne=false;
for(var i=0; i<this.httpRequestCache; i++)
{
object=HttpRequestObject(this.httpRequestCache[i]);
if (object.chunnel==_chunnel)
{
if (object.instance.readyState==0 || object.instance.readyState==4)
{
instance=object.instance;
}
getOne=true;
break;
}
}
if (!getOne) //对象不在缓存中,则创建
{
object=new HttpRequestObject();
object.chunnel=_chunnel;
object.instance=this.createInstance();
this.httpRequestCache.push(object);
instance=object.instance;
}
return instance;
}

/**
* 客户端向服务端发送请求
* @param _url:请求目的
* @param _data:要发送的数据
* @param _processRequest:用于处理返回结果的函数,其定义可以在别的地方,需要有一个参数,即要处理的通信对象
* @param _chunnel:通道名称,默认为"default"
* @param _asynchronous:是否异步处理,默认为true,即异步处理
*/
this.send=function(_url,_data,_processRequest,_chunnel,_asynchronous)
{
if (_url.length==0 || _url.indexOf("?")==0)
{
Status.showInfo("由于目的为空,请求失败,请检查!");
window.setTimeout("Status.setStatusShow(false)",3000);
return;
}
if (this.showStatus)
{
Status.showInfo("请求处理中,请稍候");
}
if (_chunnel==undefined || _chunnel=="")
{
_chunnel="default";
}
if (_asynchronous==undefined)
{
_asynchronous=true;
}
var instance=this.getInstance(_chunnel);
if (insta

上一个:[asp]天枫AJAX百度音乐即时听附下载
下一个:Baidu Musicbox 用到的ajax代码

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,