2017-03-15 71 views
-2

我想在我的網站頭部出現第一個用戶進入網站的標題。 HTML結構如下:如何只加載一次jquery

<div class="mobilebanner"> 
     <div class="mobilebanner-container"> 
      <div class="left-box"> 
       <div class="image"> 
        <img src="<?php bloginfo('template_url'); ?> /images/sample.jpg" alt=""> 
       </div> 
       <div class="text"> 
        <h4>The New Yorker Today</h4> 
        <p>Conde Nast Digital</p> 
        <div class="review-stars"> 
         <p>starts willlbe here</p> 
        </div> 
        <p>GET - On the App Store</p> 
       </div> 
      </div> 
      <div class="right-box"> 
       <div class="view"> 
        <a href="www.gmail.com">View</a> 
       </div> 
      </div> 
     </div> 
    </div> 

當我點擊圖像上,旗幟必須消失,如果用戶在24小時後訪問該網站只再次顯示。

希望有人能幫助我實現從my pen here

在此先感謝。

+4

您需要的localStorage /餅乾,沒有jQuery的 – mehulmpt

+0

@Mehul磨憨。真的不知道要這樣做。你能幫我嗎? –

+0

這是你的JS嗎? – user7417866

回答

0

這不是完整的代碼。但它會讓你去做你想做的事。

window.onload = function() { 
    if(localStorage.getItem('timestamp')) { 
    var timestampDate = new Date(parseInt(localStorage.getItem('timestamp')); 
    if(timestampDate + 24*60*60*1000 < new Date().getTime()) { // one day over 
     showBanner(); 
    } 
    } else { 
    // first visit 
    localStorage.setKey('timestamp', new Date().getTime()); 
    showBanner(); 
    } 
}; 

並顯示您的橫幅showBanner()函數。

0

使用本地存儲,並保持24小時內的約束

<script> 
if (localStorage.firsttime==1) 
{ 
var currenttime=new Date().getTime(); 
var firstvisittime=localStorage.visittime; 
var difference=currenttime-firstvisittime; 

var hoursDifference = Math.floor(difference/1000/60/60); 
if(hoursDifference>24) 
{ 
localStorage.firsttime=0; 
} 

} 

if(localStorage.firsttime==undefined||localStorage.firsttime==0) 
{ 
localStorage.firsttime=1; 
localStorage.visittime=new Date().getTime(); 
} 


</script>