Home  >  Article  >  Web Front-end  >  Javascript reads and writes XML to implement advertising rotation (compatible with IE, FF)_javascript skills

Javascript reads and writes XML to implement advertising rotation (compatible with IE, FF)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:26:03868browse

The xml structure is as follows

Copy code The code is as follows:


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< ;/dizhi>

==

// JScript file
var Now = new Date();
var Min = Now.getSeconds();
var i;
if((Min % 2) == 1)
//Read the seconds, rotate randomly according to odd and even, of course you can also change the algorithm here to increase the number of xml nodes
{
i = 1;
}
else
{ i = 0; }
var url = "xml/ads.xml";
String.prototype.Trim = function() {
return this.replace(/(^s*)|(s*$ )/g, "");
}
var xmlDoc;
//Determine whether it is Firefox or 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);
//Create XML document object under FIREFOX
}
else if (ie)
{ xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
xmlDoc.async = false;
while( xmlDoc.readyState != 4) {};
//Create XML document object under IE
}
xmlDoc.load(file);
}importXML(url);//Load 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();
//Get the value under the tupian,dizhi node
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;
// Implement rotation
} else if (ie)
{var src = xmlDoc.documentElement.selectNodes("tupian");
var href = xmlDoc.documentElement.selectNodes("dizhi");
/ /Get the value under the tupian,dizhi node
document.getElementById("dizhi").href = href(i).text;
document.getElementById("tupian").src = src(i).text ;
//Realize rotation
}}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn