2014-03-28 57 views
0

我使用的是​​3210和Eric Martin's SimpleModal。我有以下代碼:使用歷史記錄後退按鈕後關閉SimpleModal

jQuery(function() { 

    var History = window.History; 

    History.Adapter.bind(window,'statechange',function(){ 
    var State = History.getState(); 
    History.log(State.data, State.title, State.url); 
    }); 

    ('body').delegate('a', 'click', function() { 
     $.modal("<div><h1>Test</h1></div>", { 
      close: true, 
      opacity: 70, 
      onShow: function(){ 
       History.pushState({state:1}, "State 1", "?state=1"); 
      }, 
      onClose: function(){ 
       History.back(); 
       $.modal.close(); 
      } 
     }); 
    }); 
}); 

現在,如果您單擊鏈接並出現模式,它會改變它的狀態。如果關閉模式,它也可以正常工作。

唯一不起作用的是,如果您打開模式,然後使用瀏覽器的後退按鈕,它會更改狀態,但模式不會關閉。如何打開SimpleModal並關閉後退按鈕?

回答

1

試試這個:

$(window).bind("beforeunload", function() { 
    $.modal.close(); 
}); 

或使用 「window.onstatechange」 History.js事件。

+0

感謝您的回答。如果我把它放在'window.onstatechange'事件中,它會打破模式,並給我錯誤'Uncaught TypeError:Can not call method'focus'of undefined'。 –

+0

然後嘗試:$(window).on('unload',function(){$ .modal.close();}); –

+0

它沒有提供錯誤,但與以前相同的問題? –

相關問題