2012-02-15 15 views
0

我的工作,有一個Flash視頻是自動播放一旦加載頁面上的一個項目。我的客戶希望只有在有人第一次訪問該網頁時自動播放。隨後的訪問(在同一會話中)將加載視頻,但保持暫停。理解餅乾(使用jQuery插件的cookie)

Flash視頻有一個JS的功能,我可以叫暫停或播放視頻。所以我的想法是在用戶訪問該頁面後設置一個cookie,並在隨後的訪問中觸發回調。

//wait until everything has loaded 
$(window).load(function(){ 

    //set a cookie 
    $.cookie('welcome_cookie', 'welcome'); 

    //check for the cookie and call the pause function 
    if($.cookie('welcome_cookie')){ 
     controlPlayback('pause'); 
    } 
}); 

這工作,但它會暫停在第一次視頻......如何讓這個播放功能被觸發僅在返回到這個頁面?

+0

只是顛倒順序。 首先測試,然後添加cookie。 – deantoni 2012-02-15 15:02:42

回答

4

只需切換以設定第一檢查後的餅乾;

//wait until everything has loaded 
$(window).load(function(){ 

    //check for the cookie and call the pause function 
    if($.cookie('welcome_cookie')){ 
     controlPlayback('pause'); 
    } 

    //set a cookie 
    $.cookie('welcome_cookie', 'welcome'); 
}); 

你可能鴕鳥政策必須設置在每個頁面加載cookie的,爲什麼不只是設置cookie,如果it's不設置;

//check for the cookie and call the pause function 
if($.cookie('welcome_cookie')){ 
    //pause video player 
    controlPlayback('pause'); 
} else { 
    //set a cookie 
    $.cookie('welcome_cookie', 'welcome'); 
} 
+0

我之所以有$(窗口).load)是讓我知道的Flash Player嘗試調用播放功能之前加載。否則,我會得到一個未定義的Method錯誤。 – 2012-02-15 15:14:50

+0

感謝@Stefan該訣竅 – 2012-02-15 15:25:06

2

爲什麼你需要爲這個餅乾,曲奇被髮送到從服務器在每次請求和響應分別。因此,您需要在服務器上反覆發送您實際不需要的數據。你真正需要的是本地存儲。

$(window).load(function(){ 

    //If not first visit then pause 
    if(localStorage['not_first_visit']){ 
     controlPlayback('pause'); 
    } 

    //Set 
    localStorage('not_first_visit')=true; 
}); 
+0

什麼是localStorage的瀏覽器的支持?只要它能與IE7 +一起工作,我就願意嘗試這種方法。 – 2012-02-15 15:12:58

+0

IE7 +絕對支持localStorage。 – 2012-02-15 15:53:20

+0

有趣...我會試試這個...... – 2012-02-15 16:25:10