Connecting Tech Pros Worldwide Forums | Help | Site Map

redirect to another page when browser back button is clicked

Newbie
 
Join Date: Mar 2008
Posts: 6
#1: Mar 12 '08
Hi,

I am trying to redirect the user to another page (other than the previous one in the browser's history) when the browser's (IE's) BACK button is clicked. I found this snippet, but am finding it difficult in getting it to run :(

Expand|Select|Wrap|Line Numbers
  1. if (history.previous)
  2. {
  3.     document.location="page_to_go_bak_to.html";
  4. }
  5.  
Could any of you please provide a small example for the code in page2 and where it should be placed (since I am a novice :))?
scenario: user goes from page1 to page2, when he clicks on browser BACK button in page2 he goes to page_to_go_bak_to.html instead of page1.

thanks.

Familiar Sight
 
Join Date: Oct 2006
Posts: 141
#2: Mar 12 '08

re: redirect to another page when browser back button is clicked


hi,

u should write in page2 for the unload event

for example
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <title>Untitled Page</title>
  4.    <script type="text/javascript">
  5.    function func()
  6.    {
  7.    if(window.history.previous) 
  8.    {
  9.    window.navigate('HTMLPage.htm');
  10.    }
  11.    }
  12.    </script>
  13. </head>
  14. <body onunload="func()">
  15.  
  16. </body>
  17. </html>
  18.  
then it will be redirected to HTMLPage.htm
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#3: Mar 12 '08

re: redirect to another page when browser back button is clicked


you should use:

Expand|Select|Wrap|Line Numbers
  1. window.location.href = 'page_to_go_back_to.html';
instead of window.navigate() since this is not a standards-compliant method ...

kind regards
Newbie
 
Join Date: Mar 2008
Posts: 6
#4: Mar 12 '08

re: redirect to another page when browser back button is clicked


ok..i have now tried both of your suggestions - neither seems to be working
so here's my code
===================page1.html===================== ======
[HTML]<html>
<head>
<script>
function nextPage()
{
window.location.href = "page2.html"

}
</script>
<form name="next_page">
<tr>
<td align="bottom">
<form method="POST">
<p><input type="button" name="B1" value="Go to Next Page" onclick="nextPage()" style="position:absolute; top:450px; left:607px"></p>
</form>
</td>
</tr>
</form>
</head>
<body>
</body>
</html>[/HTML]
================================================== =======

===================page2.html===================== ======
[HTML]<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function func()
{
if(window.history.previous)
{
window.location.href = 'page_to_go_back_to.html';
}
}
</script>
</head>
<body onunload="func()">

</body>
</html>[/HTML]
================================================== =====

======================page_to_go_back_to.html===== ==========

[HTML]<html>
<head>
<title>Page to go bak to</title>
</head>
<body>
</body>
</html>[/HTML]
================================================== ====

please suggest - thank you.
Newbie
 
Join Date: Mar 2008
Posts: 6
#5: Mar 12 '08

re: redirect to another page when browser back button is clicked


UPDATE - the below code when put in page2.html works, but when I hit "cancel" on the dialog box, it refreshes the page2 - can this be prevented? thank you.

[HTML]<head>
<script language="Javascript">
<!--
function onBak()
{
var chk_message = "do you want to continue?";
var times = 0;
if (times == 0)
{
var leave = confirm(chk_message);
window.location.href = 'page_to_go_back_to.html';

if (!leave)
location = self.location;
times++;
}
}

//-->
</script>
</head>
<body onunload="onBak()">[/HTML]
Reply