JoJo
JoJo

Reputation: 20115

How do I redirect to a referring URL with hash values using JS?

Let's say a user navigates from greatsite.com/uno#argument to coolsite.com/dos. After some interaction on the second page, I need to send him back to his referrer with Javascript:

window.location = document.referrer;

The problem with running this is that document.referrer loses the hash values. The user gets redirected to website.com/uno without #argument. The page fails to work because its javascript depends on that argument.

Upvotes: 4

Views: 1625

Answers (3)

user964638
user964638

Reputation:

try this

window.history.go(-1)

but firefox may ask user whether to prevent

Upvotes: 0

user3
user3

Reputation: 740

what about using the history ?

if (history.back() === undefined) location.replace(document.referrer);

Upvotes: 5

DanRedux
DanRedux

Reputation: 9349

So link to dos, but append the current documents hash tag.. So dos#argument..

Then link to the referrer by appending the current hash tag to the referrer.

In other words, carry the hash tag with you through the pages.

Upvotes: 0

Related Questions