views:

693

answers:

3

How do I insert an entry into browsing history so back button goes to different page 1st click then original page on 2nd click?

So if you need a good explanation of what I want done, go to: https://secure.exitjunction.com/howitworks.jsp

I just need a script that will allow me to insert an entry in the browsing history so when back button is hit, the user will be taken to my special page.

+3  A: 

Here is a link to a jQuery Plugin:

jQuery Plugin

NTulip
there are plenty of answers on google for this. I recommended an option that uses jquery but there are straight javascript options for this.
NTulip
This is the best plugin by far to work with history, and I've worked with a number of them including a couple of standalone libraries. Even YUI's history object has some bugs in it. This one is the best of them.
altCognito
I should also note, even if you were to "write your own", I would use this one as the reference implementation.
altCognito
again - use this to get an idea for the code since you are not interested in adding google analytics support
NTulip
Thanks NTulip and altCognito - this gives me a basis to work from.
Sky
A: 

You can't directly manipulate the browsing history.

Such a feature would be seen as a security hole (and it would be), so I doubt that any browsers would ever implement it.

You might be able to hack around it however by doing something like this:

NOTE: This entirely hinges around the assumption that the referrer will get changed by the back button. I don't think this actually happens, so it more than likely won't work, but hey.

You have two pages, PageA and PageB.

  • The user hits PageA
  • The page (on the client, using javascript) checks the HTTP referrer, and if it is not PageB, then it immediately redirects the user to PageB.
  • Now that you're on PageB, if the user clicks the back button, it will go back to PageA.
  • PageA will check the referrer, it willmay be PageB, so there is no redirect.
Orion Edwards
i thought that's what he was looking for.he is looking to track exit links
NTulip
A: 

Well, many users will frown upon this behaviour because it's not what they expect.

  • User is on Page A
  • User clicks a link and it goes to page B
  • Page B detects they should be on Page C and redirects
    • while setting a cookie saying they were on Page C
    • Page B is now in history
  • User clicks Back, Page B loads and reads the cookie set by Page C

Since this is all client side, it

  • requires javascript
  • might not work all the time
  • on slow connections behaviour might be wonky.

The best way to handle this type of situation is use server-side redirects and have all 3 pages (a b c) be part of the same server-side page, which then does the necessary logic to determine what page the user should be on.

Darryl E. Clarke