15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4 ✦ The Document and Body Objects (Chapter 18)<br />

Example<br />

This demonstration requires two documents (and for IE, you’ll also need to<br />

access the documents from a Web server). The first document, in Listing 18-11, simply<br />

contains one line of text as a link to the second document. In the second document<br />

(Listing 18-12), a script verifies the document from which the user came via a<br />

link. If the script knows about that link, it displays a message relevant to the experience<br />

the user had at the first document. Also try opening Listing 18-12 in a new<br />

browser window from the Open File command in the File menu to see how the<br />

script won’t recognize the referrer.<br />

Listing 18-11: A Source Document<br />

<br />

<br />

document.referrer Property 1<br />

<br />

<br />

Visit my sister document<br />

<br />

<br />

Listing 18-12: Checking document.referrer<br />

<br />

<br />

document.referrer Property 2<br />

<br />

<br />

<br />

if(document.referrer.length > 0 &&<br />

document.referrer.indexOf(“18-11.htm”) != -1){<br />

document.write(“How is my brother document?”)<br />

} else {<br />

document.write(“Hello, and thank you for stopping by.”)<br />

}<br />

<br />

<br />

<br />

241<br />

document.referrer

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!