IE和FireFox下javascript讀寫XML實現廣告輪換

來源:互聯網
上載者:User
javascript|xml|廣告

最新動向網站首頁廣告,ASP.NET的廣告控制項很容易實現這點,可首頁是靜態頁面,聯想廣告控制項的原理決定採用javascript+xml實現這點方面配置,更新廣告時只要更新xml即可,方便了廣告輪換
xml結構如下
<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 檔案

        var  Now  =  new  Date(); 

         var  Min  =  Now.getSeconds();

        var  i;

        if((Min % 2) == 1)//讀取秒數,根據奇偶隨機輪換,當然也可以改變此處演算法增加xml節點數

  {

  i = 1;

  }

  else  {

  i = 0; 

 }

var url = "xml/ads.xml";

String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }

var xmlDoc;//判斷是否為Firefox或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);//建立FIREFOX下XML文檔對象 }  else if (ie)  {

   xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");

   xmlDoc.async = false;   while( xmlDoc.readyState != 4) {};//建立IE下XML文檔對象 }  xmlDoc.load(file);}importXML(url);//載入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();

     //得到tupian,dizhi節點下的值

     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;   //實現輪換

     } else if (ie) {

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

var href = xmlDoc.documentElement.selectNodes("dizhi");/得到tupian,dizhi節點下的值document.getElementById("dizhi").

href = href(i).text;document.getElementById("tupian").src = src(i).text;   //實現輪換 }}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.