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

Source: Internet
Author: User
Tags min trim
javascript|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 seconds, according to odd and even random rotation, of course, can also change the algorithm 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;//to determine whether 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 Firefox under XML Document object} else if (ie) {

xmldoc = new ActiveXObject ("MSXML2. domdocument.3.0 ");

Xmldoc.async = false; while (Xmldoc.readystate!= 4) {};//creates 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 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; Achieve rotation

else if (IE) {

var src = xmlDoc.documentElement.selectNodes ("Tupian");

var href = xmlDoc.documentElement.selectNodes ("Dizhi")/Get the value document.getElementById ("Dizhi") under the Tupian,dizhi node.

href = href (i). Text;document.getelementbyid ("Tupian"). src = src (i). text; Implementation rotation}}



Related Article

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.