Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Last active September 18, 2018 11:18
Show Gist options
  • Save swapnilshrikhande/d60fc656704afc1c62ddc2145f2e7271 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/d60fc656704afc1c62ddc2145f2e7271 to your computer and use it in GitHub Desktop.
Handling Back Button For Modal With An Iframe
//out side page
var clearModalState = function(){
if (window.history && window.history.pushState) {
//Grab our current Url
var url = window.location.toString();
//Remove anchor from url using the split
if( url.indexOf('#') != -1 )
window.history.replaceState({}, document.title, url.split("#")[0]);
}
}
var pushModalState = function(){
if (window.history && window.history.pushState) {
window.history.pushState('forward', null, '#modal');
}
}
var registerBackButtonHandler = function(){
if (window.history && window.history.pushState) {
$(window).on('popstate', function () {
var skipClose;
frn.closeRecordModal(skipClose=false);
});
}
}
// Initialize dialog
$(function(event){
frn.dialog = $( "#dialog-form" ).dialog({
autoOpen: false,
modal: true,
autoOpen: false,
buttons: {
// Cancel: function() {
// frn.closeRecordModal();
// }
},
beforeClose: function( event, ui ) {
//var skipClose;
frn.closeRecordModal(skipClose=true);
clearModalState();
},
open: function( event, ui ) {
//to handle back button click
pushModalState();
}
});
registerBackButtonHandler();
});
//Inside the modal page
// Handle back button click
if (window.history && window.history.pushState) {
window.history.pushState('forward', null, '#modal');
$(window).on('popstate', function () {
frn.closeParentModal();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment