15

I am trying to get the full referring url to a page via javascript.

So far the closet I have come is

var lastUrl = document.referrer;

This does not include the query attached to the url.

var lastUrl = document.URL;

returns the current URL with the querystring intact.

Is there anyway of easily obtaining the referring url along with the referring query.

4
  • Could you share us an example? Commented May 15, 2014 at 15:40
  • document.referrer will bring back for example http://mydomain.com/paths/file.php, this being the previous referring page where as I require http://mydomain.com/paths/file.php?inherit=true&id=123. Using document.URL will bring the full path but of the currently viewed page Commented May 15, 2014 at 15:47
  • This page might help you. stackoverflow.com/questions/2680328/query-string-in-javascript Commented May 15, 2014 at 15:54
  • Thanks, but I looked at these - may be able to munge the first answer to something useful though. Commented May 15, 2014 at 16:07

1 Answer 1

9

You cant. You can navigate user to previous page with

window.history.back();

but you will not know what url it was. You can get only this:

document.referrer

i know, it's not full query with url, but it's referrer.

P.s. Would you like, that any page would be able to check where you've been before? Some pages use authentication via url, so anyone would be able to grab those links from your browser's history.

Sign up to request clarification or add additional context in comments.

1 Comment

what did you mean by "any page would be able to check where you've been before" ? How does it affect the browser history, so that anyone could grab that referring link? I only want to know if it's safe to use document.URL or document.referrer inside a JS file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.