Rumah >hujung hadapan web >tutorial js >javascript读写XML实现广告轮换(兼容IE、FF)_javascript技巧

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

WBOY
WBOYasal
2016-05-16 17:26:03905semak imbas

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.getSeconds();
var  i;      
if((Min % 2) == 1)
//读取秒数,根据奇偶随机轮换,当然也可以改变此处算法增加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.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
function importXML(file) {
if (moz) 
{  
xmlDoc = document.implementation.createDocument("", "doc", null);
//创建FIREFOX下XML文档对象

else if (ie) 
{   xmlDoc = new 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 ad = xmlDoc.getElementsByTagName("ad")[0];
var tupian;
var _tupian = ad.getElementsByTagName("tupian")[i].firstChild.nodeValue;
if (_tupian)   tupian = _tupian.Trim();  
//得到tupian,dizhi节点下的值  
var dizhi;  
var _dizhi=ad.getElementsByTagName("dizhi")[i].firstChild.nodeValue; 
if(_dizhi) dizhi = _dizhi.Trim(); 
document.getElementById('tupian').href = tupian; 
document.getElementById('dizhi').src = dizhi; 
//实现轮换  
} 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;  
//实现轮换
}}

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn