Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with document.referrer.indexOf

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I'm testing a theory before using it on a production site.

I want to be able to swap images, based on the current url...so I'm thinking document.referrer.indexOf will be the easiest way to handle this.

I've come up with the following page, but it doesn't work as I expect it to work. I want it to display the homeRPG.gif image if the url contains "test" (ie or otherwise I want it to display the homeDefault.gif image. I'm testing this locally, from it should be displaying the homeDefault.gif image, but it's not. In fact I can change the rpg in ...indexOf("rpg") to anything and it still just displays the homeRPG.gif image. Can anyone show me where I'm going wrong here? Thanks!
Code:
<html>
<script>
function chkURL()
{
    if(document.referrer.indexOf("rpg") == -1)
    {
      document.getElementById('home').src = "testImgs/homeRPG.gif";
    }
}

</script>
<body onload="chkURL()">

<img src="testImgs/homeDefault.gif" name="home" id="home" />

</body>
</html>
 
Push,
document.referrer is the page that the current page was called from, try document.location or top.location (to handle frames etc).

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Thanks Greg. Going on your suggestion I looked up document.location and read the window.location is more widely accepted across browsers so I edited the code to the following, and it works!
Code:
<html>
<script>
function chkURL()
{
    if(window.location.href.indexOf("rpg") > -1)
    {
      document.getElementById('home').src = "testImgs/homeRPG.gif";
    }
}

</script>
<body onload="chkURL()">

<img src="testImgs/homeDefault.gif" name="home" id="home" />

</body>
</html>

Does this look fundamentally correct?
 
It looks good to me, although if you ever have the letters 'rpg' in any URL (even in the middle of the URL), it will still match... so if this will cause problems, you should make the search more restrictive.

Dan






Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top