2012-11-26 28 views
-2

解決方案如下:編輯#2jQuery unload():更新Cookie後的語法錯誤 - 如何找到原因?

我有一個HTML列表用戶可以排序。我不想在每次拖放操作後保存數據,因此我將這些數據保存在unload中:放在cookie和數據庫中。那是工作,但是:

保存列表隱藏在數據之後,我在這條線得到一個「語法錯誤」:

<!DOCTYPE html> 

這很奇怪,因爲一切都刷新同一個頁面後工作正常(F5)無需改變任何東西

我試圖找到原因,但沒有成功。這是流程:

1. visit the page (index.php) 
2. change the list (set: list_is_dirty = true) 
3. click any internal link (call $(window).unload(... save_data() ...) 
4. target page appears without the list (syntax error!) 
5. refreshing the page (everything works fine) 

你知道如何找到這個錯誤嗎?任何工具或策略?或者,也許卸載功能的經驗相同?

在此先感謝!

編輯:

一些代碼:

var list_is_dirty = false; 

// document ready? 
$(function() { 

    function sort_list() { 
     // some code, not important 
    } 

    sort_list(); 

    $(window).unload(function() { 
     if (list_is_dirty == true) { 

      /* ---------- HERE's the error! ---------- */ 
      /* The error occures when I try to call the script.php 
       I tried load(), $.post(), $.get() but nothing works. 
       The string is correct. I'm not even able to call any of 
       these functions without params. 
      */ 

      // send data to script.php to save data 
      $("#div").load("script.php?str="+list_data_str); 

      $.cookie("list_data", list_data_str); 
     } 
    }); 
} 

編輯#2 /解決方案: 我不知道爲什麼,但一切都與window.onbeforeunload工作jQuery.unload代替()。解釋會很棒!我很抱歉這個令人困惑的線程!

window.onbeforeunload = function(e) { 
    $("#div").load("script.php"); 
} 
+3

需要更多的實際代碼/錯誤,少說釋義。 – melpomene

+0

@melpomene感謝您的回覆。我試圖找出重要的路線。 –

+0

@aug我認爲你是對的。這是我猜想的。我會在幾分鐘後發佈一些代碼。謝謝! –

回答

1

我認爲你的問題是:list_data_str,因爲它沒有在任何地方定義。 如果你想說你想要做AJAX發佈例如,那麼顯然你需要尋找成功事件 否則它看起來你的演示代碼缺少的東西,因爲你可以做你的方式嘗試,如果在在URL上使用$ _GET的接收腳本不關注任何參數。換句話說,您錯過了該對象,並且當您刷新頁面時,它將被加載到DOM中。顯然,這可能是你所描述的問題,我建議你發佈一些與你的問題代碼相關的更多內容。比如接收腳本或來自像Firebug這樣的調試器的任何錯誤。

關於如何測試它,您可能希望在受支持的瀏覽器中使用console.log,或者在設置cookie時進行簡單警報。

  var global list_is_dirty = false; 

       function sort_list(list, list_is_dirty) { 
       // some code, not important 
      //check the list and the flag 
      //you should return a value, else it does not make sense to use a function here.. note the var defined as global 
      return list; //?? not sure what to return as don't know what this code does from the posting 
      } 



       jQuery(function($) 
       { 

       $(window).load(function(e){ 

           var list_data_str= sort_list(); 


         // send data to script.php to save data 
         e("#div").load('script.php?str='+list_data_str); 

            //on unload destroy the cookie perhaps?? or if it's not set a session variable 
         e.cookie("list_data", list_data_str); 

       ... 


          The unload event 

           $(window).unload(function(e) { 


           $("#div").load("script.php?str="+list_data_str); 

           $.cookie("list_data", list_data_str); 
            } 

           }); 
          } 
          .... 

//關於您的編輯:你在這裏傳遞任何參數到腳本?因爲我認爲問題在於這個邏輯。 (「#div」)。load(「script.php」);

+0

感謝您的詳細解答!該代碼不是獨立工作的,我只是發佈它給你一個相當的概述。我有這個想法使用window.onbeforeload從這個網頁:http://forum.jquery.com/topic/jquery-ajax-and-unload –

+0

有趣的...我很想看到一個演示,如果你可以把它放在一起在jsfiddle ..我相信,如果你想打開一個模式對話窗口,還有一些其他的小細節你應該考慮。例如使它具有唯一的ID以避免多個實例被隱藏等等。當時,也許如果你正在尋找的是檢查對話模塊是否被卸載,這將更有意義..在事件中你正在進行一些處理,比如從對話窗口提交表單等等。無論如何,這是一個有趣的話題。 –