Home > Article > Web Front-end > Javascript reads and writes XML to implement advertising rotation (compatible with IE, FF)_javascript skills
The xml structure is as follows
// 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
}}