Back button "PING-PONG" effect (instead of going back like..when using browsers back button)

Back button "PING-PONG" effect (instead of going back like..when using browsers back button)

SITE EXAMPLE: A1-B-C, A2-B-C, A3-B-C

B has "back button"
C has "back button"


From the site example you can see that you can come to B from multiple pages: A1, A2, A3

B's back button is implemented like this:

  1. $("#goback").click(function(event)
        {
            event.preventDefault();
            window.location=document.referrer;
        });       





And will navigate you back to the page from where you came to B (A1,A2,A3) -> that is correct.

NOW THE PROBLEM:

From B you can go to C

C has the same back button implementation and navigates you back to B -> that is correct
And now, when you clik on B's backbutton, instead of going to: A1, A2 or A3  you go back to C

And a Ping-Pong effects is made.

The same happenz if I use: history.go(-1); history.go(-2),..etc


What should i DO?

thx