is there a way to understand if a page is navigated by pressing back ?

  • Thread starter Ersin Gençtürk
  • Start date
E

Ersin Gençtürk

I show errors as popup boxes via alert() but I write this scripts in header
of html page.So if there is an error on the server side , in the next
postback a popup shows the error via alert , this works fine.But when user
presses the back button then the former error messages appear ,because pages
are loaded from the cache.I need to find a way in javascript if a page is
loaded from server or navigated to page by pressing back or forward
buttons.Is there a property or anything that I can do ?
 
J

Jeff B

There are a couple of things you could try:

You won't be able to 'disable' the back button per se but rather take an
appropriate action upon detecting its use. To detect its use you can try
either a client or server side approach depending on your needs.

The back button does not map to a JavaScript DOM event. However, you can
trap the 'onload' event of the body tag and take the appropriate action
which effectively gives you the same thing.

Alternatively, on the server side, you can disable output caching using a
page directive in your aspx's thus:

<%@ OutputCache Location="None" %>

Disabling the output cache forces the browser to retreive the page again
from the server which the server can detect and handle as appropriate.
Without it the server will not receive any notification that the end-user
just navigated.

Most web MVC frameworks have support for this type of navigation problem,
along with bookmarking random pages (and hence jumping around) your website,
typing in and navigating to a random URL, use of the back button etc. In
most cases the solution requires the use of stateful URLs. The Microsoft
User Interface Process v2 application block has rudimentary back button
support.

The only form of 'disablement' I can think of that you can do is catch all
user keystrokes client side, like 'ALT-<leftarrow>' (which is the moral
equivalent of hitting the back button), by using

setCapture() and treating it as a no-op.
 
E

Ersin Gençtürk

hi jeff,

thanks for the reply.I forgot but it is not a solution for my case to
disable the cache.On the other hand, I put a onload event on body and
pressed back button of the browser , page came from the cache but also the
onload event fired again.Am I doing something wrong ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top