Jump to content

Back Button


holmedwa04

Recommended Posts

Hi,I have a website, as you probaly can see from my signature, and on most pages I have back buttons, the only problem is that when somebody enters a page of my site, from google perhaps, their favorites, or by typing in the URL, if it is not the index page then when they click a back button it either doesn't work, or they get shunted off my site.So my question is, how do I make it go to my homepage when any of the above occur?This is my code:

<form> <input type="Button" value="Back" onclick="history.back()"> </form>

Link to comment
Share on other sites

Just simply make it link to the correct file instead of using the history :)This is fairly enough:

<input type="button" value="Back" onclick="location.href='myindex.htm'" />

This is even shorter:

<button onclick="location.href='myindex.htm'">Back</button>

Link to comment
Share on other sites

@Dan The idea is to link back to the previous page in history when a record in it exist. Returning to the home page should only occur when the script otherwise leads nowhere.

Link to comment
Share on other sites

There is definetly a way to do this, I have a system on my server right now that has the referer, then you can just click a link to go back to it. Im not sure how its done but I can try to find out.BTW: its done with php :)EDIT:found it, but it will require you to add some code on every page. All you need to do is make a file called referrer.php or something and include it on every page.heres the code I have, but its in a class:

	  /* Set referrer page */	  if(isset($_SESSION['url'])){		 $this->referrer = $_SESSION['url'];	  }else{		 $this->referrer = "/";	  }	  /* Set current url */	  $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];

So you see where im getting at. You will need to tweak it to your needs though.Basically it checks to see if $_SESSION['url'] has been set, and if it isnt it means the person accessed the site from like google. Then it does something for that, or else it sets the referrer variable to the current page.

Link to comment
Share on other sites

You can't unfortunately :)You can't target the items in the history with javascript, only its go(), back() and forward() functions, and its length. :)At least that is what W3Schools says. There are no more properties or methods for the history object other than those four that I know of.I would also like to be able to condition the items in the history, but I think there is no other way than what I posted above, if you are not using server-side scripting. Sorry :)

Link to comment
Share on other sites

I think I have an idea. What does the

history.back()

return when there's no record? I mean, it must return something... if it's an empty value, then wouldn't something like:

var history = history.back() if (history !== '') {history.back()}else{location.href='myindex.htm'}

work?[edit]@holmedwa04, not the code I gave. It's a really raw form of my thoughts and this form needs to be accessed differently. I'm waiting for someone more experienced at JS to give this piece of $hit a $hot.[/edit]

Link to comment
Share on other sites

Better is to check if the url in the history item matches the url of this site, but that is not possible. history.back() doesn't return anything though. And neither can it be stored in a variable :)[*Edit:]No, that code didn't work. I used it in a valid way, but the variable inwhichs history.back() gets stored, produces an error saying "undefined is empty." Meaning it can't store it. :)

Link to comment
Share on other sites

http://www.w3schools.com/htmldom/dom_obj_history.aspFor empty 'history', check the length for the History object:
<html><head><script>function goBack(){if (history.length != 0) {	history.back();	} else {	location.href='http://www.google.com';	}}</script></head><body><a href="#" onclick="goBack(); return false;">BACK</a></body></html>

Link to comment
Share on other sites

http://www.w3schools.com/htmldom/dom_obj_history.aspFor empty 'history', check the length for the History object:
<html><head><script>function goBack(){if (history.length != 0) {	history.back();	} else {	location.href='http://www.google.com';	}}</script></head><body><a href="#" onclick="goBack(); return false;">BACK</a></body></html>

So, I add the script to my head section, and replace the google, with my site, and then add the link?Also can I change it so that it is a form style button?Edit: Ah, yeah, that seems to work, when I open the page from clicking on it, but when I type the URL in it goes back to google when I click it, is there some way to make it think that google isn't a page? Or add google to its list of "dont go back to"? Apart from that it works though.Edit 2: I have got it to a button now, so no worrys about that, just the bit about bypassing google.Edit 3: Even better, I have just thought, cant you change a parameter so that it goes back so many pages, so perhaps, or maybe not, it wont think that any page other than on of mine will work. Ok, now I am even confusing myself!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...