2013-01-23 36 views
0

我想要找到如何設置JavaScript cookie,以便在瀏覽第4頁的瀏覽量之後顯示fancybox彈出窗口。JS cookie用於在第x頁面瀏覽後顯示內容

這裏是我使用的顯示的fancybox彈出的代碼,但你可以看到,這顯示只在第一次網頁瀏覽的代碼,它每天

function setCookie(c_name,value,exdays) 
{ 
    var exdate=new Date(); 
    exdate.setDate(exdate.getDate() + exdays); 
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); 
    document.cookie=c_name + "=" + c_value + ";domain=.mysite.net;path=/"; 
} 

function getCookie(c_name) 
{ 
    var i,x,y,ARRcookies=document.cookie.split(";"); 
    for (i=0;i<ARRcookies.length;i++) 
    { 
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); 
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); 
    x=x.replace(/^\s+|\s+$/g,""); 
     if (x==c_name) 
     { 
     return unescape(y); 
     } 
    } 
} 

jQuery(document).ready(function() { 
    var show = getCookie("fancyreg"); 
    if(show==null || show=="") { 
     $.fancybox(
{ 
     'type' : 'iframe', 
     'href' : 'http://www.mysite.net/popup/', //URL to popup page or image 
     'autoDimensions' : false, 
     'width' : 480, 
     'height' : 260, 
     'transitionIn' : 'none', 
     'transitionOut' : 'none' 
} 
); 

    setCookie('fancyreg','1',1); 

} 
}); 

我也想,如果到期後您可以幫助我如何在現有代碼中添加延遲,以便在3秒(3000毫秒)後彈出顯示。

我試着用的setTimeout(函數(),如下

<script type="text/javascript"> 
jQuery(document).ready(function() { 
    setTimeout(function() { 
     $.fancybox({ 
      'type' : 'iframe', 
     'href' : 'http://www.mysite.net/popup/', //URL to popup page or image 
     'autoDimensions' : false, 
     'width' : 480, 
     'height' : 260, 
     'transitionIn' : 'none', 
     'transitionOut' : 'none' 
     }) 
    }, 4000); 
}); 
</script> 

,但它不能正常工作。

至於控制瀏覽量,我不知道如何設置,也沒有我能找到的任何資源幫助我通過這個。

非常感謝

+0

我們不是代碼猴子。你有什麼嘗試? –

+0

嗨,Jan,我通過使用setTimeout JS函數添加了彈出延遲的代碼。 – valdroni

+0

您可能需要使用jQuery cookie插件。查看http://stackoverflow.com/a/8305703/1055987獲取更多信息。您可以在4天內設置cookie過期,然後打開fancybox – JFK

回答

3

假設你正在使用的jQuery Cookie Plugin,那麼你可以使用下面的代碼來啓動3秒,在同一天的第4頁參觀後的fancybox:

$(document).ready(function() { 
    // create cookie 
    var visited = $.cookie('visited'); // visited = 0 
    if (visited >= 3) { 
     // open fancybox after 3 secs on 4th visit or further on the same day 
     setTimeout(function() { 
      $.fancybox.open({ 
       // your fancybox API options here 
      }); 
     }, 3000); 
    } else { 
     visited++; // increase counter of visits 
     // set new cookie value to match visits 
     $.cookie('visited', visited, { 
      expires: 1 // expires after one day 
     }); 
     return false; 
    } 
}); // ready 

working DEMO

+0

非常好。這很好,就像我想的那樣。先生,你是搖滾明星! – valdroni