JavaScript reads and writes XML realizes advertisement rotation (compatible IE, FF) _javascript skill

Source: Internet
Author: User

The XML structure is as follows

Copy Code code 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 the number of seconds, depending on the parity random rotation, of course, you can also change the algorithm to increase the XML node points
{
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) {};
Create an IE XML Document object
}
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;
Achieve 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;
Achieve 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.