Javascript: javascript reads and writes XML in IE and FireFox to achieve AD rotation. Javascript tutorial
I recently updated my website homepage advertisement, ASP. NET ad controls can easily achieve this, but the homepage is a static page. Lenovo's ad Control Principle decides to use javascript + xml to implement this configuration. When updating an advertisement, you only need to update xml, convenient ad Rotation
The xml structure is as follows:
Http://edu.ITbulo.com/UploadFiles_1485/200610/20061010132448597.jpg
Http://www.seehaha.com/plan/
Http://www.seehaha.com/va.htm
=
// JScript file var Now = new Date (); var Min = Now. getSeconds (); var I; if (Min % 2) = 1) // The number of read seconds, 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 an XML Document Object in 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 xmlfunction updatetupiandizhi () {if (moz) {var ad = xmlDoc. getElementsByTagName ("ad") [0]; var tupian; var _ tupian = ad. getElementsByTagName ("tupian") [I]. firstChild. nodeValue; if (_ tupian) tupian = _ tupian. trim (); // obtain the value var dizhi and var _ dizhi = ad under the tupian and dizhi nodes. getElementsByTagName ("dizhi") [I]. firstChild. nodeValue; if (_ dizhi) dizhi = _ dizhi. trim (); document. getElementById ('tuian '). href = tupian; document. getElementById ('dizhi '). src = dizhi; // implement rotation} else if (ie) {var src = xmlDoc.doc umentElement. selectNodes ("tupian"); var href = xmlDoc.doc umentElement. selectNodes ("dizhi"); // obtain the value document under the tupian and dizhi nodes. getElementById ("dizhi "). href = href (I ). text; document. getElementById ("tupian "). src = src (I ). text; // rotate }}