Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

document.referrer and document.location difference?

What is the emphasis of difference between document.referrer and document.location in Javascript.

Wasn't quite clear to me from MDN besides one resembles URI and the second, URL (maybe also accepts methods).

1 Answer

Hello Ben,

Document Referrer Return A URL of Page Before Redirect To This Page. For Example :

You Start with Page http://example.com/ and Click The Link To Your Website or Embed Your Website in The Iframe : http://bentheexample.com

In Your Website : benthexample.com , The Document Referrer Code Will Return The The URL That Refer To You = http://bentheexample.com

However , The Document Location Will Return Your Current URL :

http://bentheexample.com

References :

https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer

https://developer.mozilla.org/en-US/docs/Web/API/Document/location

You can browse around the web and Run This Script on Google Chrome Inspector Console :

For Depth Tutorial about Chrome Inspector Tools Workshop : https://teamtreehouse.com/library/website-optimization/chrome-devtools-basics/the-console-panel-2

To Open a Inspector Console on Google Chrome : Right Click -> Inspect and Click on Console Between The Element and Sources and Paste These Code Below in to console. Document Location

alert(document.location);

Document Referrer

alert(document.referrer);

Thanks.

Hi! I know the console and MDN. I read on document.referrer on MDN but didn't quite understand. You explanation wasn't quite clear to me either so I would thank you for rephrasing it (only the part on document.referrer), maybe with another, more intuitive example. Juthawong Naisanguansee

Hi Ben Aharoni ,

Document Referrer is like Treehouse Referral Program . It return what URL that bring you to this page.

I now totally understand the concept, thank you.