利用cookie實現“只彈出一次視窗”的JS代碼

來源:互聯網
上載者:User

標籤:需要   set   var   length   控制   substring   代碼   script   get   

彈出式視窗通常被用來做彈出廣告(CPM),其實用彈出式視窗用來做訊息通知也是最普遍而且是最有效方法,但如果每次重新整理頁面特別是重新整理首頁都要快顯視窗的話,那絕對是讓訪問者厭煩的事情。

比如你將上面的指令碼放在一個需要頻繁經過的頁面裡(例如首頁),那麼每次重新整理這個頁面,視窗都會彈出一次,我們使用cookie來控制一下就可以了。 

原理:編寫代碼查看當前訪問者電腦上的Cookie中是否包含有本網站的資訊,如果有,則此電腦已經不是第一次訪問首頁,再次瀏覽首頁時無需彈出廣告視窗;否則,就彈出廣告。

 

<script>   window.onload=loadpopup();  function openwin(){   window.open("page.html","","width=200,height=200")   }   function get_cookie(Name) {   var search = Name + "="   var returnvalue = "";   if (document.cookie.length > 0) {   offset = document.cookie.indexOf(search)   if (offset != -1) {   offset += search.length   end = document.cookie.indexOf(";", offset);   if (end == -1)   end = document.cookie.length;   returnvalue=unescape(document.cookie.substring(offset, end))   }   }   return returnvalue;   }    function loadpopup(){   if (get_cookie(‘popped‘)==‘‘){   openwin()   document.cookie="popped=yes"   }   }   </script>

  

利用cookie實現“只彈出一次視窗”的JS代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.