Change value for browser back button?

I have a script where I want the browser’s back button to reload the current page, NOT go back to the previous URL.

I figured it’s not possible to attach my own functions to clicking the back button, so I though the next best thing is to manipulate what the latest url is in the history list. Is this possible?

Read this: http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

:slight_smile:

One thing to remember is that onbeforeunload is not a standard event and therefor is only supported by some browsers. Also in those browsers that do support it some people will have become so annoyed at all the “are you sure you want to leave this page” messages that they will have disabled it.

An alternate way that will work except where your visitors have meta redirects disabled would be to set up two web pages where the first consists of nothing except an immediate redirect to the second (plus a link for those with meta redirects disabled). Pressing the back button on the second page will go back to the first page which will immediately redirect back to the second page.

Thanks for the info :slight_smile:

If I choose the redirect method, could I just use:

<script type=“text/javascript”>
window.location = “http://www.whatever.com”;
</script>

Any ideas on the percentage of users with meta redirects disabled?

I’d imagine more people would have JavaScript disabled than would have meta redirects disabled and so the meta redirect would be more likely to work that the JavaScript version.

<meta http-equiv=“refresh” content=“0;URL=http://www.example.com”>

You could use the JavaScript one as well just to cover for if your visitor has meta redirects disabled but still allows JavaScript.

I also just read something like these or similar types of redirects are SEO killers. One source I read said the site wouldn’t be indexed if the search engine came across a redirect. Do you have any experience with this problem?

I’m doing something wrong…

this is in the head of the document:


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta http-equiv="refresh" content="0;URL=http://www.chris-rawlins.com/codenesi/print-repair-servicescopy.html">
		<title></title>
		<script src="main.js" type="text/javascript"></script>
		<link href="main.css" rel="stylesheet" type="text/css">
		<script type="text/javascript">
			window.location = "http://www.chris-rawlins.com/codenesi/print-repair-servicescopy.html";
		</script>

To see the issue go here click “repair services”. Repair services is print-repair-services.html - the page that has the redirects. It redirects you to print-repair-servicescopy.html. Now click the ‘home/office inkjet’ link and try clicking back. It will take you to print-home.html, NOT print-repair-servicescopy.html…

See if commenting out the JavaScript redirect makes any difference.

I tried commenting out the js but nothing changed.

I noticed that refreshing the page will put it in history and create the situation I need for the back button to refresh the current page and not actually go back.

Now this short simple script I wrote is looping, it shouldn’t loop. It’s in an external file.


var counter = 0;

if( counter == 0 )
{
	alert(counter); //alerts 0
	counter++;
	alert(counter); //alerts 1
	window.location.reload();	
}

I must have tunnel vision, why is this script looping? Why doesn’t var counter get reset to 0 making the if statement run again?

What does it take to get a URL to file in the browser’s history?? Now on print-repair-services.html I have a redirect on window.onload that redirects to print-repair-servicescopy.html and print-repair-services.html still isn’t listed in the history…

That depends on the type of redirect that’s performed.
If it’s a meta-refresh redirect then the browser often believes that it shouldn’t store the original page in its history. There’s no point, as it is of no use and you should be going to the redirected page instead.

If you use some javascript though to change the location.href to another page, then the original will be kept in the history.

Yahoo has a browser history manager. Looks like a good option as well. Most (+95%) of my visitors have javascript.

I’m not sure why you’re doing this at all, and to be honest I hate going to websites when I have to repeatedly click the back button to get back to previous pages - I’m sure other web users find it equally as annoying. - Have you considered usability?

If you use some javascript though to change the location.href to another page, then the original will be kept in the history.

I’m sure the error is on my part…

If you go to here and click “repair services”.

Repair services (or, print-repair-services.html) is the link/file that has this redirect:


window.onload = init;

function init(){
	location.href = "http://www.chris-rawlins.com/codenesi/print-repair-servicescopy.html";
}

in an external file.

So the link “repair services” links to print-repair-services.html which redirects you to print-repair-servicescopy.html. And when I check the history when I’m on print-repair-servicescopy.html it is this page not the page that I was redirected from.

I’m looking at the print-repair-services.html page now and the external redirect script doesn’t appear to correctly exist.

:slight_smile:
Sorry, I did take it out. It’s back now. So, the issue from post 15 is still there.

I think I should explain what I was trying to do with all of this redirect code.

If you go here and click any of the thumbnails in the main content area (home/office inkjet, multifunction, etc), you’ll notice that only the content in the main content area changes - the sidebar info, footer, etc stay the same.

I thought I would be able to use js to only, for example if scanners was clicked, change the scanner corresponding main content information, instead of making separate html files that include all the same code for the header, sidebar, etc.

If javascript was disabled there would be default html files available, so maybe I should just use separate html files. If the redirect idea didn’t work I figured to use js I’d have to put a button in the main content area as a trigger to reload the original print-repair-services.html page info but I’d rather not do that.

Part of me thinks since I’m trying to use js to reload content on a different page its not the right tool for the job.