IE and Firefox JavaScript read and write XML to achieve AD rotation

Source: Internet
Author: User
Tags min trim
xml| Advertising

Recently updated the home page ads, ASP. NET advertising control is easy to achieve this, but the homepage is a static page, Lenovo advertising control principle decided to use javascript+xml to achieve this aspect of configuration, update the ads as long as the XML can be updated to facilitate the advertising rotation
The XML structure is as follows
<ad>
<tupian>http://www.seehaha.com/images/060901-150.100.2.gif</tupian>
<tupian>http://www.seehaha.com/xml/061009-300.100.jpg</tupian>
<dizhi>http://www.seehaha.com/plan/</dizhi>
<dizhi>http://www.seehaha.com/va.htm</dizhi>
</ad>
==

JScript files         var  now  =  new  Date ();           var  min  =  now.getseconds ();         var  i;        if (Min% 2) = = 1)//read number of seconds, according to odd and even random rotation, Of course, you can change this. The algorithm increases 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;//to determine whether Firefox or ie var Moz = (typeof document.implementation!= ' undefined ')    && (typeof doc Ument.implementation.createDocument!= ' undefined '); var ie = (typeof window.) ActiveXObject!= ' undefined '); function ImportXML (file) {if (Moz)   {   xmldoc = Document.implementation.createDocument ("", "Doc", null);//create Firefox XML Document Object}  else if (ie)   {   xmldoc = new ActiveXObject ("MSXML2. domdocument.3.0 ");   Xmldoc.async = false;   while (xmldoc.readystate!= 4) {};//Create an IE XML Document object}   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 ();    //Get Tupian,dizhi node value      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;  //implementation rotation     } else if (ie) {var src = XmlDoc.documentElement.selectNodes ("Tupian"); var href = xmlDoc.documentElement.selectNodes ("Dizhi");/Get TuThe value document.getElementById ("Dizhi") under the Pian,dizhi node. href = href (i). Text;document.getelementbyid ("Tupian"). src = src (i) .text;  //implementation rotation  }



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.