首頁  >  文章  >  web前端  >  javascript讀寫XML實作廣告輪替(相容IE、FF)_javascript技巧

javascript讀寫XML實作廣告輪替(相容IE、FF)_javascript技巧

WBOY
WBOY原創
2016-05-16 17:26:03867瀏覽

xml結構如下

複製程式碼 程式碼如下:

http://www.seehaha.com/images/060901-150.100.2.gif
http://www.seehaha.com/xml/061009-300.100.jpg
http://www.seehaha.com/plan/
http://www.seehaha.com/va.​​htm

==
// JScript 檔案   

var  Now  =  new  Date();   
var  Min  =  Now.getSec  
//讀取秒數,根據奇偶隨機輪換,當然也可以改變此處演算法增加xml節點數 
{
i = 1;

else
{  i = 0;  }
var url = "xml/ads.xml";
String.prototype.Trim = function() {
return this.replace(/(^s*)|(s*$ )/g, "");
}
var xmlDoc;
//判斷是否為Firefox或IE
var moz = (typeof document.implementation != 'undefined')   & (typeof document.implementation != 'undefined')   && (typeof. .implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
function importXML(file) {
if (moz)  🎜>xmlDoc = document.implementation.createDocument("", "doc", null);
//建立FIREFOX下XML文件物件

else if (ie))
ActiveXObject("MSXML2.DOMDocument.3.0");
xmlDoc.async = false;
while( xmlDoc.readyState != 4) {};
//建立IE下XML文件物件
}
}
}
}
}
}
xmlDoc.load(file);
}importXML(url);//載入xml
function updatetupiandizhi()
{
if (moz)
{
var
if (moz)
{
var ad = xmlDoc.getElementsByTagName("ad")[0];
var tupian;
var _tupian = ad.getElementsByTagName("tupian")[i].firstChild.nodecon;
if (_tupian) = _tupian.Trim();  
//得到tupian,dizhi節點下的值  
var dizhi;  
var _dizhi=ad.getElementsByTagName("dizhi")[Chii].nohio")[ >if(_dizhi) dizhi = _dizhi.Trim(); 
document.getElementById('tupian').href = tupian; 
document.getElementById('dizhi').src = dizhio/o🎠實作輪換  
} else if (ie)
{var src = xmlDoc.documentElement.selectNodes("tupian");
var href = xmlDoc.documentElement.selectNodes("dizhi"); /得到tupian,dizhi節點下的值
document.getElementById("dizhi").href = href(i).text;
document.getElementById("tupian").src = src(i).text ;  
//實現輪換
}}

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn