zl程序教程

您现在的位置是:首页 >  前端

当前栏目

javascript读写XML实现广告轮换(兼容IE、FF)

JavaScriptXML 实现 读写 兼容 IE 广告 FF
2023-06-13 09:15:04 时间

xml结构如下

复制代码代码如下:

<ad>
<tupian>http://www.seehaha.com/images/060901-150.100.2.gif</tupian>
<tupian>http://www.seehaha.com/xml/061009-300.100.jpg</tupian>
<dizhi>http://www.seehaha.com/plan/</dizhi>
<dizhi>http://www.seehaha.com/va.htm</dizhi>
</ad>
==

//JScript文件   
var Now = new Date();   
var Min = Now.getSeconds();
var i;      
if((Min%2)==1)
//读取秒数,根据奇偶随机轮换,当然也可以改变此处算法增加xml节点数 
{
i=1;

else
{ i=0; }
varurl="xml/ads.xml";
String.prototype.Trim=function(){
returnthis.replace(/(^\s*)|(\s*$)/g,"");
}
varxmlDoc;
//判断是否为Firefox或IE
varmoz=(typeofdocument.implementation!="undefined")  &&(typeofdocument.implementation.createDocument!="undefined");
varie=(typeofwindow.ActiveXObject!="undefined");
functionimportXML(file){
if(moz) 
{  
xmlDoc=document.implementation.createDocument("","doc",null);
//创建FIREFOX下XML文档对象

elseif(ie) 
{  xmlDoc=newActiveXObject("MSXML2.DOMDocument.3.0");
xmlDoc.async=false;
while(xmlDoc.readyState!=4){};
//创建IE下XML文档对象

xmlDoc.load(file);
}importXML(url);//载入xml
functionupdatetupiandizhi()
{
if(moz)
{
varad=xmlDoc.getElementsByTagName("ad")[0];
vartupian;
var_tupian=ad.getElementsByTagName("tupian")[i].firstChild.nodeValue;
if(_tupian)  tupian=_tupian.Trim();  
//得到tupian,dizhi节点下的值  
vardizhi;  
var_dizhi=ad.getElementsByTagName("dizhi")[i].firstChild.nodeValue; 
if(_dizhi)dizhi=_dizhi.Trim(); 
document.getElementById("tupian").href=tupian; 
document.getElementById("dizhi").src=dizhi; 
//实现轮换  
}elseif(ie)
{varsrc=xmlDoc.documentElement.selectNodes("tupian");
varhref=xmlDoc.documentElement.selectNodes("dizhi");
//得到tupian,dizhi节点下的值
document.getElementById("dizhi").href=href(i).text;
document.getElementById("tupian").src=src(i).text;  
//实现轮换
}}