Javascript reading and writing XML in IE and FireFox to achieve AD rotation _ Javascript tutorial

Source: Internet
Author: User
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 }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.