Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close Overlay when user presses browser "Back" button. #46

Open
jampafoo opened this issue Jan 26, 2015 · 2 comments
Open

Close Overlay when user presses browser "Back" button. #46

jampafoo opened this issue Jan 26, 2015 · 2 comments

Comments

@jampafoo
Copy link

Wondering if you would consider adding functionality which allows the user to close overlay via the browser’s “back” button. OR, if there is already an option to do this?

It seems to be more of an issue on mobile browsers. But it preforms the same on desktop also. Thanks!

@Nishithkant
Copy link

You may use following :

window.onhashchange = function () {
        $(".youOverlayClass").fadeIn(500);
        $('body')
      .css('overflow', 'hidden');
    } 
@EvanOlsen
Copy link

Here's how I accomplished it. The use case here is a modal window for details on books for sale.

$('.book-modal').popup({
    onopen: function() {
    	//Set a hash using the modal's ID
        window.history.pushState("", document.title, window.location.pathname + '#' + $(this).attr("id"));
	    window.onhashchange = function () {
	    	//Close all modals on hash change
            $('.book-modal').popup('hide');
            //Make the browser go "back" so you don't fill up the history state
            window.history.back();
        } 
    },
    onclose: function() {
    	//Reset the URL to remove the modal ID
        window.history.pushState("", document.title, window.location.pathname);
    }
});

if (window.location.hash) {
	//If there's a hash in the URL
    bookID = window.location.hash;
    //Show the modal with the same ID as the hash
    $(bookID).popup('show');
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants