JavaScript - close popup with back buttonIntercepting call to the back button in my AJAX applicationUse window.location.href to go 2 pages back and reloadHow do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
How can I deal with uncomfortable silence from my partner?
If I leave the US through an airport, do I have to return through the same airport?
bash does not know the letter 'p'
How creative should the DM let an artificer be in terms of what they can build?
Why can I traceroute to this IP address, but not ping?
My boss want to get rid of me - what should I do?
How come the nude protesters were not arrested?
Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?
Is using 'echo' to display attacker-controlled data on the terminal dangerous?
Is it possible for a vehicle to be manufactured without a catalytic converter?
How can I make 12 tone and atonal melodies sound interesting?
What's the difference between the Add and Linear Dodge blend modes in After Effects?
How to safely destroy (a large quantity of) valid checks?
Are polynomials with the same roots identical?
How can one's career as a reviewer be ended?
Should I refuse being named as co-author of a bad quality paper?
Should I put programming books I wrote a few years ago on my resume?
Teaching a class likely meant to inflate the GPA of student athletes
Why do American speakers pronounce "the" as "/ðə/" before vowels?
Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?
Cardinal exponentiations inequality
Why not invest in precious metals?
Is it a bad idea to to run 24 tap and shock lands in standard
Why am I Seeing A Weird "Notch" on the Data Line For Some Logical 1s?
JavaScript - close popup with back button
Intercepting call to the back button in my AJAX applicationUse window.location.href to go 2 pages back and reloadHow do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a custom popup functionality. What I want is for the browser back button to close this popup.
My ideal scenario would be to NOT show a hashtag in the URL bar.
I have tried putting window.history.pushState('forward', null, ''); in my showPopup() function and then doing the following:
$(window).on('popstate', function ()
closePopup();
);
This does work but the problem is when I manually close the popup I have to press the back button twice to navigate back to the previous page (obviously because a browser history entry was added when the popup was opened).
What is the best way of doing this? Can it be done without adding a browser history entry? Essentially what I am trying to do is replicate the behaviour of a mobile app. Press the back button in a mobile app will usually dismiss any open modals or context menus.
$('.popup-link').click(function()
showPopup();
);
$('.popup-close').click(function()
hidePopup();
);
function showPopup()
$('.popup').addClass('active');
function hidePopup()
$('.popup').removeClass('active');
.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>javascript jquery html html5
add a comment |
I have a custom popup functionality. What I want is for the browser back button to close this popup.
My ideal scenario would be to NOT show a hashtag in the URL bar.
I have tried putting window.history.pushState('forward', null, ''); in my showPopup() function and then doing the following:
$(window).on('popstate', function ()
closePopup();
);
This does work but the problem is when I manually close the popup I have to press the back button twice to navigate back to the previous page (obviously because a browser history entry was added when the popup was opened).
What is the best way of doing this? Can it be done without adding a browser history entry? Essentially what I am trying to do is replicate the behaviour of a mobile app. Press the back button in a mobile app will usually dismiss any open modals or context menus.
$('.popup-link').click(function()
showPopup();
);
$('.popup-close').click(function()
hidePopup();
);
function showPopup()
$('.popup').addClass('active');
function hidePopup()
$('.popup').removeClass('active');
.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>javascript jquery html html5
1
Changing standard behaviour is terrible user experience. Why would you do such a thing?
– Adrian Brand
Mar 26 at 22:36
1
Well you could argue this "standard behaviour" is bad UX - since a lot of users now (especially mobile app users) are accustomed to pressing back to dismiss popups and prompts.
– GSTAR
Mar 26 at 23:05
Well you would be arguing an incorrect argument.
– Adrian Brand
Mar 26 at 23:08
What do you mean bymanually close the popup? Do you mean closing the popup usingclosebutton with classpopup-close?
– pouyan
Mar 27 at 7:34
1
Even though your problem does not exist anymore or has been solved otherwise, you should follow up the answers posted.
– Munim Munna
Apr 3 at 14:59
add a comment |
I have a custom popup functionality. What I want is for the browser back button to close this popup.
My ideal scenario would be to NOT show a hashtag in the URL bar.
I have tried putting window.history.pushState('forward', null, ''); in my showPopup() function and then doing the following:
$(window).on('popstate', function ()
closePopup();
);
This does work but the problem is when I manually close the popup I have to press the back button twice to navigate back to the previous page (obviously because a browser history entry was added when the popup was opened).
What is the best way of doing this? Can it be done without adding a browser history entry? Essentially what I am trying to do is replicate the behaviour of a mobile app. Press the back button in a mobile app will usually dismiss any open modals or context menus.
$('.popup-link').click(function()
showPopup();
);
$('.popup-close').click(function()
hidePopup();
);
function showPopup()
$('.popup').addClass('active');
function hidePopup()
$('.popup').removeClass('active');
.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>javascript jquery html html5
I have a custom popup functionality. What I want is for the browser back button to close this popup.
My ideal scenario would be to NOT show a hashtag in the URL bar.
I have tried putting window.history.pushState('forward', null, ''); in my showPopup() function and then doing the following:
$(window).on('popstate', function ()
closePopup();
);
This does work but the problem is when I manually close the popup I have to press the back button twice to navigate back to the previous page (obviously because a browser history entry was added when the popup was opened).
What is the best way of doing this? Can it be done without adding a browser history entry? Essentially what I am trying to do is replicate the behaviour of a mobile app. Press the back button in a mobile app will usually dismiss any open modals or context menus.
$('.popup-link').click(function()
showPopup();
);
$('.popup-close').click(function()
hidePopup();
);
function showPopup()
$('.popup').addClass('active');
function hidePopup()
$('.popup').removeClass('active');
.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>$('.popup-link').click(function()
showPopup();
);
$('.popup-close').click(function()
hidePopup();
);
function showPopup()
$('.popup').addClass('active');
function hidePopup()
$('.popup').removeClass('active');
.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>$('.popup-link').click(function()
showPopup();
);
$('.popup-close').click(function()
hidePopup();
);
function showPopup()
$('.popup').addClass('active');
function hidePopup()
$('.popup').removeClass('active');
.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>javascript jquery html html5
javascript jquery html html5
edited Mar 25 at 21:20
GSTAR
asked Mar 24 at 19:45
GSTARGSTAR
1,4671158102
1,4671158102
1
Changing standard behaviour is terrible user experience. Why would you do such a thing?
– Adrian Brand
Mar 26 at 22:36
1
Well you could argue this "standard behaviour" is bad UX - since a lot of users now (especially mobile app users) are accustomed to pressing back to dismiss popups and prompts.
– GSTAR
Mar 26 at 23:05
Well you would be arguing an incorrect argument.
– Adrian Brand
Mar 26 at 23:08
What do you mean bymanually close the popup? Do you mean closing the popup usingclosebutton with classpopup-close?
– pouyan
Mar 27 at 7:34
1
Even though your problem does not exist anymore or has been solved otherwise, you should follow up the answers posted.
– Munim Munna
Apr 3 at 14:59
add a comment |
1
Changing standard behaviour is terrible user experience. Why would you do such a thing?
– Adrian Brand
Mar 26 at 22:36
1
Well you could argue this "standard behaviour" is bad UX - since a lot of users now (especially mobile app users) are accustomed to pressing back to dismiss popups and prompts.
– GSTAR
Mar 26 at 23:05
Well you would be arguing an incorrect argument.
– Adrian Brand
Mar 26 at 23:08
What do you mean bymanually close the popup? Do you mean closing the popup usingclosebutton with classpopup-close?
– pouyan
Mar 27 at 7:34
1
Even though your problem does not exist anymore or has been solved otherwise, you should follow up the answers posted.
– Munim Munna
Apr 3 at 14:59
1
1
Changing standard behaviour is terrible user experience. Why would you do such a thing?
– Adrian Brand
Mar 26 at 22:36
Changing standard behaviour is terrible user experience. Why would you do such a thing?
– Adrian Brand
Mar 26 at 22:36
1
1
Well you could argue this "standard behaviour" is bad UX - since a lot of users now (especially mobile app users) are accustomed to pressing back to dismiss popups and prompts.
– GSTAR
Mar 26 at 23:05
Well you could argue this "standard behaviour" is bad UX - since a lot of users now (especially mobile app users) are accustomed to pressing back to dismiss popups and prompts.
– GSTAR
Mar 26 at 23:05
Well you would be arguing an incorrect argument.
– Adrian Brand
Mar 26 at 23:08
Well you would be arguing an incorrect argument.
– Adrian Brand
Mar 26 at 23:08
What do you mean by
manually close the popup? Do you mean closing the popup using close button with class popup-close?– pouyan
Mar 27 at 7:34
What do you mean by
manually close the popup? Do you mean closing the popup using close button with class popup-close?– pouyan
Mar 27 at 7:34
1
1
Even though your problem does not exist anymore or has been solved otherwise, you should follow up the answers posted.
– Munim Munna
Apr 3 at 14:59
Even though your problem does not exist anymore or has been solved otherwise, you should follow up the answers posted.
– Munim Munna
Apr 3 at 14:59
add a comment |
7 Answers
7
active
oldest
votes
It is not possible to do it without adding browser history entries since you cannot override the back button behaviour, see Intercepting call to the back button in my AJAX application: I don't want it to do anything
Sujumayas answer is a good option, you should introduce some additional variable though to avoid problems with the history when opening multiple popups (e.g. when clicking the button multiple times)
Here is some possible sample code:
let popupOpen = false;
$(".popup-link").click(function()
showPopup();
);
$(".popup-close").click(function()
window.history.back();
);
function showPopup()
if (popupOpen)
window.history.back();
popupOpen = true;
window.history.pushState("forward", null, "");
$(".popup").addClass("active");
function hidePopup()
popupOpen = false;
$(".popup").removeClass("active");
$(window).on("popstate", function()
hidePopup();
);
Additionally please note that you might have problems with Opera Mini: https://caniuse.com/#search=history
@GSTAR I think this is what you are looking for.
– Munim Munna
Mar 27 at 15:51
add a comment |
Altho I don't recommend to override regular browser history managment (back button) to use it as you please....
I think that the only thing you missed in your example is that the close button should not close the modal by itself, but instead just execute a backbutton event (which will eventually close the modal).
That simple fix and it will work as you wanted.
add a comment |
You would have two options to implement this:
Option 1: Using the window.beforeunload event. reference
$('.popup-link').click(function()
showPopup();
$(window).on("beforeunload", hidePopup);
);
$('.popup-close').click(hidePopup);
function hidePopup()
$(window).off("beforeunload", hidePopup);
$('.popup').removeClass('active');
Demo
Option 2: Using the HTML5 History API. reference
$('.popup-link').click(function()
showPopup();
window.history.pushState('popup-open', null, '');
$(window).on('popstate', hidePopup);
);
$('.popup-close').click(function()
if(history.state == 'popup-open')
window.history.back();
hidePopup();
);
function hidePopup()
$(window).off('popstate', hidePopup);
$('.popup').removeClass('active');
Demo
Edit: sujumayas's idea is also pretty good one.
Demo
Further, I'ld recommend to register the popstate / beforeunload events only when necessary and unregister them, when you no longer need 'em in order to avoid overhead.
add a comment |
I am doing already something like this, and it works nicely with the browser back-button and by pushing the android back-button as well. I am also not showing a hashtag in the URL bar.
Here is the stub (I just tried to apply that to Your scenario):
function freezeHistory()
window.history.pushState(, window.document.title, window.location.href);
function goBack()
/*
Custom history back actions: close panel, close popup, close drop-down menu
*/
var popupOpen = $(".popup.active").length > 0;
if(popupOpen)
hidePopup();
return false;
window.history.back();
return true;
function showPopup()
$('.popup').addClass('active');
freezeHistory();
function hidePopup()
$('.popup').removeClass('active');
$(window).on("popstate", function(e)
/*
Browsers tend to handle the popstate event differently on page load.
Chrome (prior to v34) and Safari always emit a popstate event on page load,
but Firefox doesn’t.
*/
goBack();
)
If this won't work for You out-of-the box, it is because IMHO You may need to clarify a little bit how do You expect to manage the page history. Feel free to add more detail to Your question if this isn't working as You'd expect now, but anyway, I strongly believe You got the idea and You are able to apply it inside the scenario of Your web-app.
add a comment |
Open popup and try going back and forth with the browser history buttons
$(document).ready(function ()
// manage popup state
var poped = false;
$('.popup-link').click(function ()
// prevent unwanted state changtes
if(!poped)
showPopup();
);
$('.popup-close').click(function ()
// prevent unwanted state changtes
if(poped)
hidePopup();
);
function showPopup()
poped = true;
$('.popup').addClass('active');
// push a new state. Also note that this does not trigger onpopstate
window.history.pushState('poped': poped, null, '');
function hidePopup()
poped = false;
// go back to previous state. Also note that this does not trigger onpopstate
history.back();
$('.popup').removeClass('active');
);
// triggers when browser history is changed via browser
window.onpopstate = function(event)
// show/hide popup based on poped state
if(event.state && event.state.poped)
$('.popup').addClass('active');
else
$('.popup').removeClass('active');
;.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>add a comment |
You could add window.history.go(-2) to your popstate. That should take you back twice, which would be your original page before the modal as pushState added an entry to your history object.
Conversely, you could use history.back(2)
Use window.location.href to go 2 pages back and reload
add a comment |
Just run window.history.back(); when closing the popup.
$('.popup-close').click(function()
hidePopup();
window.history.back();
);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327862%2fjavascript-close-popup-with-back-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is not possible to do it without adding browser history entries since you cannot override the back button behaviour, see Intercepting call to the back button in my AJAX application: I don't want it to do anything
Sujumayas answer is a good option, you should introduce some additional variable though to avoid problems with the history when opening multiple popups (e.g. when clicking the button multiple times)
Here is some possible sample code:
let popupOpen = false;
$(".popup-link").click(function()
showPopup();
);
$(".popup-close").click(function()
window.history.back();
);
function showPopup()
if (popupOpen)
window.history.back();
popupOpen = true;
window.history.pushState("forward", null, "");
$(".popup").addClass("active");
function hidePopup()
popupOpen = false;
$(".popup").removeClass("active");
$(window).on("popstate", function()
hidePopup();
);
Additionally please note that you might have problems with Opera Mini: https://caniuse.com/#search=history
@GSTAR I think this is what you are looking for.
– Munim Munna
Mar 27 at 15:51
add a comment |
It is not possible to do it without adding browser history entries since you cannot override the back button behaviour, see Intercepting call to the back button in my AJAX application: I don't want it to do anything
Sujumayas answer is a good option, you should introduce some additional variable though to avoid problems with the history when opening multiple popups (e.g. when clicking the button multiple times)
Here is some possible sample code:
let popupOpen = false;
$(".popup-link").click(function()
showPopup();
);
$(".popup-close").click(function()
window.history.back();
);
function showPopup()
if (popupOpen)
window.history.back();
popupOpen = true;
window.history.pushState("forward", null, "");
$(".popup").addClass("active");
function hidePopup()
popupOpen = false;
$(".popup").removeClass("active");
$(window).on("popstate", function()
hidePopup();
);
Additionally please note that you might have problems with Opera Mini: https://caniuse.com/#search=history
@GSTAR I think this is what you are looking for.
– Munim Munna
Mar 27 at 15:51
add a comment |
It is not possible to do it without adding browser history entries since you cannot override the back button behaviour, see Intercepting call to the back button in my AJAX application: I don't want it to do anything
Sujumayas answer is a good option, you should introduce some additional variable though to avoid problems with the history when opening multiple popups (e.g. when clicking the button multiple times)
Here is some possible sample code:
let popupOpen = false;
$(".popup-link").click(function()
showPopup();
);
$(".popup-close").click(function()
window.history.back();
);
function showPopup()
if (popupOpen)
window.history.back();
popupOpen = true;
window.history.pushState("forward", null, "");
$(".popup").addClass("active");
function hidePopup()
popupOpen = false;
$(".popup").removeClass("active");
$(window).on("popstate", function()
hidePopup();
);
Additionally please note that you might have problems with Opera Mini: https://caniuse.com/#search=history
It is not possible to do it without adding browser history entries since you cannot override the back button behaviour, see Intercepting call to the back button in my AJAX application: I don't want it to do anything
Sujumayas answer is a good option, you should introduce some additional variable though to avoid problems with the history when opening multiple popups (e.g. when clicking the button multiple times)
Here is some possible sample code:
let popupOpen = false;
$(".popup-link").click(function()
showPopup();
);
$(".popup-close").click(function()
window.history.back();
);
function showPopup()
if (popupOpen)
window.history.back();
popupOpen = true;
window.history.pushState("forward", null, "");
$(".popup").addClass("active");
function hidePopup()
popupOpen = false;
$(".popup").removeClass("active");
$(window).on("popstate", function()
hidePopup();
);
Additionally please note that you might have problems with Opera Mini: https://caniuse.com/#search=history
answered Mar 26 at 23:11
Stefan BlambergStefan Blamberg
674420
674420
@GSTAR I think this is what you are looking for.
– Munim Munna
Mar 27 at 15:51
add a comment |
@GSTAR I think this is what you are looking for.
– Munim Munna
Mar 27 at 15:51
@GSTAR I think this is what you are looking for.
– Munim Munna
Mar 27 at 15:51
@GSTAR I think this is what you are looking for.
– Munim Munna
Mar 27 at 15:51
add a comment |
Altho I don't recommend to override regular browser history managment (back button) to use it as you please....
I think that the only thing you missed in your example is that the close button should not close the modal by itself, but instead just execute a backbutton event (which will eventually close the modal).
That simple fix and it will work as you wanted.
add a comment |
Altho I don't recommend to override regular browser history managment (back button) to use it as you please....
I think that the only thing you missed in your example is that the close button should not close the modal by itself, but instead just execute a backbutton event (which will eventually close the modal).
That simple fix and it will work as you wanted.
add a comment |
Altho I don't recommend to override regular browser history managment (back button) to use it as you please....
I think that the only thing you missed in your example is that the close button should not close the modal by itself, but instead just execute a backbutton event (which will eventually close the modal).
That simple fix and it will work as you wanted.
Altho I don't recommend to override regular browser history managment (back button) to use it as you please....
I think that the only thing you missed in your example is that the close button should not close the modal by itself, but instead just execute a backbutton event (which will eventually close the modal).
That simple fix and it will work as you wanted.
answered Mar 24 at 19:53
sujumayassujumayas
758
758
add a comment |
add a comment |
You would have two options to implement this:
Option 1: Using the window.beforeunload event. reference
$('.popup-link').click(function()
showPopup();
$(window).on("beforeunload", hidePopup);
);
$('.popup-close').click(hidePopup);
function hidePopup()
$(window).off("beforeunload", hidePopup);
$('.popup').removeClass('active');
Demo
Option 2: Using the HTML5 History API. reference
$('.popup-link').click(function()
showPopup();
window.history.pushState('popup-open', null, '');
$(window).on('popstate', hidePopup);
);
$('.popup-close').click(function()
if(history.state == 'popup-open')
window.history.back();
hidePopup();
);
function hidePopup()
$(window).off('popstate', hidePopup);
$('.popup').removeClass('active');
Demo
Edit: sujumayas's idea is also pretty good one.
Demo
Further, I'ld recommend to register the popstate / beforeunload events only when necessary and unregister them, when you no longer need 'em in order to avoid overhead.
add a comment |
You would have two options to implement this:
Option 1: Using the window.beforeunload event. reference
$('.popup-link').click(function()
showPopup();
$(window).on("beforeunload", hidePopup);
);
$('.popup-close').click(hidePopup);
function hidePopup()
$(window).off("beforeunload", hidePopup);
$('.popup').removeClass('active');
Demo
Option 2: Using the HTML5 History API. reference
$('.popup-link').click(function()
showPopup();
window.history.pushState('popup-open', null, '');
$(window).on('popstate', hidePopup);
);
$('.popup-close').click(function()
if(history.state == 'popup-open')
window.history.back();
hidePopup();
);
function hidePopup()
$(window).off('popstate', hidePopup);
$('.popup').removeClass('active');
Demo
Edit: sujumayas's idea is also pretty good one.
Demo
Further, I'ld recommend to register the popstate / beforeunload events only when necessary and unregister them, when you no longer need 'em in order to avoid overhead.
add a comment |
You would have two options to implement this:
Option 1: Using the window.beforeunload event. reference
$('.popup-link').click(function()
showPopup();
$(window).on("beforeunload", hidePopup);
);
$('.popup-close').click(hidePopup);
function hidePopup()
$(window).off("beforeunload", hidePopup);
$('.popup').removeClass('active');
Demo
Option 2: Using the HTML5 History API. reference
$('.popup-link').click(function()
showPopup();
window.history.pushState('popup-open', null, '');
$(window).on('popstate', hidePopup);
);
$('.popup-close').click(function()
if(history.state == 'popup-open')
window.history.back();
hidePopup();
);
function hidePopup()
$(window).off('popstate', hidePopup);
$('.popup').removeClass('active');
Demo
Edit: sujumayas's idea is also pretty good one.
Demo
Further, I'ld recommend to register the popstate / beforeunload events only when necessary and unregister them, when you no longer need 'em in order to avoid overhead.
You would have two options to implement this:
Option 1: Using the window.beforeunload event. reference
$('.popup-link').click(function()
showPopup();
$(window).on("beforeunload", hidePopup);
);
$('.popup-close').click(hidePopup);
function hidePopup()
$(window).off("beforeunload", hidePopup);
$('.popup').removeClass('active');
Demo
Option 2: Using the HTML5 History API. reference
$('.popup-link').click(function()
showPopup();
window.history.pushState('popup-open', null, '');
$(window).on('popstate', hidePopup);
);
$('.popup-close').click(function()
if(history.state == 'popup-open')
window.history.back();
hidePopup();
);
function hidePopup()
$(window).off('popstate', hidePopup);
$('.popup').removeClass('active');
Demo
Edit: sujumayas's idea is also pretty good one.
Demo
Further, I'ld recommend to register the popstate / beforeunload events only when necessary and unregister them, when you no longer need 'em in order to avoid overhead.
edited Apr 2 at 19:18
answered Apr 2 at 18:50
user6576367
add a comment |
add a comment |
I am doing already something like this, and it works nicely with the browser back-button and by pushing the android back-button as well. I am also not showing a hashtag in the URL bar.
Here is the stub (I just tried to apply that to Your scenario):
function freezeHistory()
window.history.pushState(, window.document.title, window.location.href);
function goBack()
/*
Custom history back actions: close panel, close popup, close drop-down menu
*/
var popupOpen = $(".popup.active").length > 0;
if(popupOpen)
hidePopup();
return false;
window.history.back();
return true;
function showPopup()
$('.popup').addClass('active');
freezeHistory();
function hidePopup()
$('.popup').removeClass('active');
$(window).on("popstate", function(e)
/*
Browsers tend to handle the popstate event differently on page load.
Chrome (prior to v34) and Safari always emit a popstate event on page load,
but Firefox doesn’t.
*/
goBack();
)
If this won't work for You out-of-the box, it is because IMHO You may need to clarify a little bit how do You expect to manage the page history. Feel free to add more detail to Your question if this isn't working as You'd expect now, but anyway, I strongly believe You got the idea and You are able to apply it inside the scenario of Your web-app.
add a comment |
I am doing already something like this, and it works nicely with the browser back-button and by pushing the android back-button as well. I am also not showing a hashtag in the URL bar.
Here is the stub (I just tried to apply that to Your scenario):
function freezeHistory()
window.history.pushState(, window.document.title, window.location.href);
function goBack()
/*
Custom history back actions: close panel, close popup, close drop-down menu
*/
var popupOpen = $(".popup.active").length > 0;
if(popupOpen)
hidePopup();
return false;
window.history.back();
return true;
function showPopup()
$('.popup').addClass('active');
freezeHistory();
function hidePopup()
$('.popup').removeClass('active');
$(window).on("popstate", function(e)
/*
Browsers tend to handle the popstate event differently on page load.
Chrome (prior to v34) and Safari always emit a popstate event on page load,
but Firefox doesn’t.
*/
goBack();
)
If this won't work for You out-of-the box, it is because IMHO You may need to clarify a little bit how do You expect to manage the page history. Feel free to add more detail to Your question if this isn't working as You'd expect now, but anyway, I strongly believe You got the idea and You are able to apply it inside the scenario of Your web-app.
add a comment |
I am doing already something like this, and it works nicely with the browser back-button and by pushing the android back-button as well. I am also not showing a hashtag in the URL bar.
Here is the stub (I just tried to apply that to Your scenario):
function freezeHistory()
window.history.pushState(, window.document.title, window.location.href);
function goBack()
/*
Custom history back actions: close panel, close popup, close drop-down menu
*/
var popupOpen = $(".popup.active").length > 0;
if(popupOpen)
hidePopup();
return false;
window.history.back();
return true;
function showPopup()
$('.popup').addClass('active');
freezeHistory();
function hidePopup()
$('.popup').removeClass('active');
$(window).on("popstate", function(e)
/*
Browsers tend to handle the popstate event differently on page load.
Chrome (prior to v34) and Safari always emit a popstate event on page load,
but Firefox doesn’t.
*/
goBack();
)
If this won't work for You out-of-the box, it is because IMHO You may need to clarify a little bit how do You expect to manage the page history. Feel free to add more detail to Your question if this isn't working as You'd expect now, but anyway, I strongly believe You got the idea and You are able to apply it inside the scenario of Your web-app.
I am doing already something like this, and it works nicely with the browser back-button and by pushing the android back-button as well. I am also not showing a hashtag in the URL bar.
Here is the stub (I just tried to apply that to Your scenario):
function freezeHistory()
window.history.pushState(, window.document.title, window.location.href);
function goBack()
/*
Custom history back actions: close panel, close popup, close drop-down menu
*/
var popupOpen = $(".popup.active").length > 0;
if(popupOpen)
hidePopup();
return false;
window.history.back();
return true;
function showPopup()
$('.popup').addClass('active');
freezeHistory();
function hidePopup()
$('.popup').removeClass('active');
$(window).on("popstate", function(e)
/*
Browsers tend to handle the popstate event differently on page load.
Chrome (prior to v34) and Safari always emit a popstate event on page load,
but Firefox doesn’t.
*/
goBack();
)
If this won't work for You out-of-the box, it is because IMHO You may need to clarify a little bit how do You expect to manage the page history. Feel free to add more detail to Your question if this isn't working as You'd expect now, but anyway, I strongly believe You got the idea and You are able to apply it inside the scenario of Your web-app.
edited Mar 28 at 3:36
answered Mar 27 at 22:03
deblockerdeblocker
5,42721436
5,42721436
add a comment |
add a comment |
Open popup and try going back and forth with the browser history buttons
$(document).ready(function ()
// manage popup state
var poped = false;
$('.popup-link').click(function ()
// prevent unwanted state changtes
if(!poped)
showPopup();
);
$('.popup-close').click(function ()
// prevent unwanted state changtes
if(poped)
hidePopup();
);
function showPopup()
poped = true;
$('.popup').addClass('active');
// push a new state. Also note that this does not trigger onpopstate
window.history.pushState('poped': poped, null, '');
function hidePopup()
poped = false;
// go back to previous state. Also note that this does not trigger onpopstate
history.back();
$('.popup').removeClass('active');
);
// triggers when browser history is changed via browser
window.onpopstate = function(event)
// show/hide popup based on poped state
if(event.state && event.state.poped)
$('.popup').addClass('active');
else
$('.popup').removeClass('active');
;.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>add a comment |
Open popup and try going back and forth with the browser history buttons
$(document).ready(function ()
// manage popup state
var poped = false;
$('.popup-link').click(function ()
// prevent unwanted state changtes
if(!poped)
showPopup();
);
$('.popup-close').click(function ()
// prevent unwanted state changtes
if(poped)
hidePopup();
);
function showPopup()
poped = true;
$('.popup').addClass('active');
// push a new state. Also note that this does not trigger onpopstate
window.history.pushState('poped': poped, null, '');
function hidePopup()
poped = false;
// go back to previous state. Also note that this does not trigger onpopstate
history.back();
$('.popup').removeClass('active');
);
// triggers when browser history is changed via browser
window.onpopstate = function(event)
// show/hide popup based on poped state
if(event.state && event.state.poped)
$('.popup').addClass('active');
else
$('.popup').removeClass('active');
;.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>add a comment |
Open popup and try going back and forth with the browser history buttons
$(document).ready(function ()
// manage popup state
var poped = false;
$('.popup-link').click(function ()
// prevent unwanted state changtes
if(!poped)
showPopup();
);
$('.popup-close').click(function ()
// prevent unwanted state changtes
if(poped)
hidePopup();
);
function showPopup()
poped = true;
$('.popup').addClass('active');
// push a new state. Also note that this does not trigger onpopstate
window.history.pushState('poped': poped, null, '');
function hidePopup()
poped = false;
// go back to previous state. Also note that this does not trigger onpopstate
history.back();
$('.popup').removeClass('active');
);
// triggers when browser history is changed via browser
window.onpopstate = function(event)
// show/hide popup based on poped state
if(event.state && event.state.poped)
$('.popup').addClass('active');
else
$('.popup').removeClass('active');
;.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>Open popup and try going back and forth with the browser history buttons
$(document).ready(function ()
// manage popup state
var poped = false;
$('.popup-link').click(function ()
// prevent unwanted state changtes
if(!poped)
showPopup();
);
$('.popup-close').click(function ()
// prevent unwanted state changtes
if(poped)
hidePopup();
);
function showPopup()
poped = true;
$('.popup').addClass('active');
// push a new state. Also note that this does not trigger onpopstate
window.history.pushState('poped': poped, null, '');
function hidePopup()
poped = false;
// go back to previous state. Also note that this does not trigger onpopstate
history.back();
$('.popup').removeClass('active');
);
// triggers when browser history is changed via browser
window.onpopstate = function(event)
// show/hide popup based on poped state
if(event.state && event.state.poped)
$('.popup').addClass('active');
else
$('.popup').removeClass('active');
;.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>$(document).ready(function ()
// manage popup state
var poped = false;
$('.popup-link').click(function ()
// prevent unwanted state changtes
if(!poped)
showPopup();
);
$('.popup-close').click(function ()
// prevent unwanted state changtes
if(poped)
hidePopup();
);
function showPopup()
poped = true;
$('.popup').addClass('active');
// push a new state. Also note that this does not trigger onpopstate
window.history.pushState('poped': poped, null, '');
function hidePopup()
poped = false;
// go back to previous state. Also note that this does not trigger onpopstate
history.back();
$('.popup').removeClass('active');
);
// triggers when browser history is changed via browser
window.onpopstate = function(event)
// show/hide popup based on poped state
if(event.state && event.state.poped)
$('.popup').addClass('active');
else
$('.popup').removeClass('active');
;.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>$(document).ready(function ()
// manage popup state
var poped = false;
$('.popup-link').click(function ()
// prevent unwanted state changtes
if(!poped)
showPopup();
);
$('.popup-close').click(function ()
// prevent unwanted state changtes
if(poped)
hidePopup();
);
function showPopup()
poped = true;
$('.popup').addClass('active');
// push a new state. Also note that this does not trigger onpopstate
window.history.pushState('poped': poped, null, '');
function hidePopup()
poped = false;
// go back to previous state. Also note that this does not trigger onpopstate
history.back();
$('.popup').removeClass('active');
);
// triggers when browser history is changed via browser
window.onpopstate = function(event)
// show/hide popup based on poped state
if(event.state && event.state.poped)
$('.popup').addClass('active');
else
$('.popup').removeClass('active');
;.popup
background-color: #ccc;
width: 300px;
height: 300px;
display: none;
.popup.active
display: block;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="popup-link">Click</button>
<div class="popup">
<button class="popup-close">x</button>
<!-- popup content here -->
</div>edited Mar 29 at 21:05
answered Mar 29 at 20:57
LasithdsLasithds
627926
627926
add a comment |
add a comment |
You could add window.history.go(-2) to your popstate. That should take you back twice, which would be your original page before the modal as pushState added an entry to your history object.
Conversely, you could use history.back(2)
Use window.location.href to go 2 pages back and reload
add a comment |
You could add window.history.go(-2) to your popstate. That should take you back twice, which would be your original page before the modal as pushState added an entry to your history object.
Conversely, you could use history.back(2)
Use window.location.href to go 2 pages back and reload
add a comment |
You could add window.history.go(-2) to your popstate. That should take you back twice, which would be your original page before the modal as pushState added an entry to your history object.
Conversely, you could use history.back(2)
Use window.location.href to go 2 pages back and reload
You could add window.history.go(-2) to your popstate. That should take you back twice, which would be your original page before the modal as pushState added an entry to your history object.
Conversely, you could use history.back(2)
Use window.location.href to go 2 pages back and reload
answered Apr 1 at 14:20
Amy Victoria ShacklesAmy Victoria Shackles
111
111
add a comment |
add a comment |
Just run window.history.back(); when closing the popup.
$('.popup-close').click(function()
hidePopup();
window.history.back();
);
add a comment |
Just run window.history.back(); when closing the popup.
$('.popup-close').click(function()
hidePopup();
window.history.back();
);
add a comment |
Just run window.history.back(); when closing the popup.
$('.popup-close').click(function()
hidePopup();
window.history.back();
);
Just run window.history.back(); when closing the popup.
$('.popup-close').click(function()
hidePopup();
window.history.back();
);
answered Apr 2 at 16:47
DaWeDaWe
212
212
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327862%2fjavascript-close-popup-with-back-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Changing standard behaviour is terrible user experience. Why would you do such a thing?
– Adrian Brand
Mar 26 at 22:36
1
Well you could argue this "standard behaviour" is bad UX - since a lot of users now (especially mobile app users) are accustomed to pressing back to dismiss popups and prompts.
– GSTAR
Mar 26 at 23:05
Well you would be arguing an incorrect argument.
– Adrian Brand
Mar 26 at 23:08
What do you mean by
manually close the popup? Do you mean closing the popup usingclosebutton with classpopup-close?– pouyan
Mar 27 at 7:34
1
Even though your problem does not exist anymore or has been solved otherwise, you should follow up the answers posted.
– Munim Munna
Apr 3 at 14:59