Is there any way to stop a web page from refreshing when it is displayed after someone presses the back button?
My web page shows different information every time it is refreshed. I want it to show whatever it was showing before the user moved to another page.
Is this possible? If so how?
No.
The contents of the window when the 'back' button is pressed is actually data your browser has cached so preventing 'back' is not going to help.
You could maybe use a hash though.
go to
stackoverflow.com
then go to
stackoverflow.com#foo
then go to
stackoverflow.com#foobar
If you hit the back button you'll notice the hash changes but the page does not refresh.
Sorry about the formatting. Blame SO!
Related
We have some forms that users fill out. If during the process the navigate away I need to perform an action.
I've found many examples of combining onbeforeunload and unload in the browser to prompt the user the user and remind them that if they close or navigate away the information they have entered won't be saved. That part is working well.
The only problem I still have is if the user reloads -- right-click reload, F5, or browser refresh button -- then the unload is also called. This is a problem for us.
I found information about the performance.navigation property. But the values of that aren't available until after the page reloads, that is, it tells me that the page I'm on has been reloaded, but not that the page is about to be reloaded.
Is there any way to detect an impending refresh as opposed to a navigate away?
No.
I recommend you "perform an action" as your user is filling out the form. Then if the user chooses to leave, no harm done.
It's perfectly feasible to post this information to a server as it is being populated. If the "perform an action" requires interaction with the user, you should just do that interaction on beforeunload.
EDIT: At the point of beforeunload, the only interaction that can be provided is an alert() asking the user to OK / Cancel navigating away from the page.
Our site has an issue where the browser back button is not returning you to the page you navigated from. Specifically, this occurs when you click a link to navigate to our home page and the browser back button is then not working. We need a temporary fix to this problem until we can correctly resolve this issue.
So, is it possible to have some jquery or javascript that does the following:
If a user is on a specific page (for example, www.abc.com/index.htm), and the user clicks the browser back button, force the browser to display the page www.xyz.com?
I am not a software developer so my knowledge of coding is fairly rudimentary and I do know that changing the default behavior of the browser back button is not good practice.
Thats makes me crazy.
I am calling details page from primary page, using, javascript - window.open("4prod.html?pkd="+idd,'_self', false); that open in the same window page with product details. When this details page has been opened there is a button "back", again in javascript -onclick="history.go(-1); return false;.
In FF works great, it means, that on "back" button from details page, the primary page appears with state as I left it, but in Chrome and IE "back" button reload primary page and all entries (inputs, color changes etc. which users have made) have gone and new - refreshed page is displayed.
How I can avoid reloading in Chrome nad IE just call of cached primary page ?
This answer says it's impossible without cookies if the URL changes. Is it possible to just have the form in one <div>, then hide it (and show the details in another <div>), then when the back button is clicked, just reverse this? If anything, (once the page has loaded the JS) the execution would be faster.
I'd like to execute a javascript (or jQuery) initialization function -- let's call it myPageInit() after a page is fully loaded.
Here's the catch: myPageInit() should run only when the page is initially loaded or if the user forces a page refresh (i.e. presses the browser's reload button, hits F5, etc.).
It should not run if the user follows a link from the page and then hits the back button.
For the avoidance of doubt:
User navigates to www.mypage.com => myPageInit() runs after page is loaded.
User now hits browser refresh button => myPageInit() runs after page is reloaded.
User now clicks on a link on the page (e.g. <a href="www.cnn.com">... ).
User clicks back button to get back to www.mypage.com => myPageInit() does not run after page is shown.
What I've tried so far
$(window).load() -- I think this used to work, but on modern browsers this appears to fire when the user presses the back button (see the comments on this issue)
$(window).ready() -- This also appears to fire on both initial load and back button.
Searching SO for other relevant answers. I still haven't found what I'm looking for.
Thanks in advance!
Maybe not exactly what you needed, but how about a session cookie or HTML storage flag? See code and answers at:
e.g. create session based cookie, jquery
How would I prevent the browser from opening the pop up window that was displayed from the previous page when the user clicks the back button?
I hope that made sense but I'll explain it in point form below:
There are three pages: Page1, Page2, Page3
User loads Page1 and clicks a link to load Page2 in a new window using JavaScript (i.e. window.open(...)). The User can close Page2 anytime they wish-- but we'll assume the user does so before step 2.
User now clicks a link on Page1 to load Page3.
User is now on Page3 and clicks the back button on their browser.
Page1 is displayed in the browser but Page2 is displayed again in a pop up window.
So, I'm wondering if there is a way to prevent Page2 from popping up again. I am using classic ASP as well, if it matters.
If Page2 is opened as a result of clicking a link, how is it automatically being opened when the back button is clicked? Unless Page2 is opened automatically on the initial load of Page1, it shouldn't pop up due to the back button being pressed - pressing "Back" doesn't click a link on the previous page. I suspect something else is going on in your page - can we see some code please?
you can try creating a session variable on page2. and check if it exist upon opening of page1. or use a cookie.
EDIT:
Function IsPostBack()
IsPostBack = (Request.ServerVariables("REQUEST_METHOD") = "POST")
End Function
this will not detect a page refresh though..
The page was redirecting to itself and inserting the javascript code to pop up a new window based on the button pressed previously. I think it determined this via the post or get methods. I forget now.
As such, when the user presses the back button, they will reload the page with the embedded javascript code and not the original page.
We've since left classic ASP behind so I can't really test any suggestions but appreciate the help.