Connecting Tech Pros Worldwide Help | Site Map

Dynamically change an image based on document.URL

Newbie
 
Join Date: Mar 2008
Posts: 8
#1: Mar 28 '08
Hello,


The objective of this script is to show visitors who found my site by clicking on one of our Google Adwords a specific telephone number (in graphic form) for the sake of tracking.


Here is the code I have been working on. My issue lies within the document.URL area of the script. I tried this code without declaring the source variable, but the script would redirect me to the document.URL declaration.


After running this code on a test page, it now displays the first image in the series, but should be showing the second image as the page I am viewing it on does not have the URL tied to it.






<script language = “javascript” type=”text/javascript”>
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. function adWords()
  4.  
  5.  
  6.  
  7. {
  8.  
  9.  
  10.  
  11. source=document.URL     <!-- If I eliminate this declaration and just use document.URL in the IF statement, the page gets redirected to that URL
  12.  
  13.  
  14.  
  15. if(source="http://www.appletreeanswers.com/index.html") { 
  16.  
  17.  
  18.  
  19. document.write("<IMG SRC='/normalimage.jpg'>")
  20.  
  21.  
  22.  
  23. }else{
  24.  
  25.  
  26.  
  27. document.write("<IMG SRC='/adwordimage.jpg'>")
  28.  
  29.  
  30.  
  31.  }
  32.  
  33.  
  34.  
  35. }
  36.  
  37.  
  38.  
  39.  
-->

</script>





<script language="javascript" type="text/javascript">



adWords();



</script>


Thanks!
poe poe is offline
Member
 
Join Date: Jun 2007
Location: Tennessee
Posts: 32
#2: Mar 29 '08

re: Dynamically change an image based on document.URL


The reason the page gets redirected is because your if statement is making an assignment instead of a comparison.

What you have: blah = blah2

What you need: blah == blah2

With what you have, as soon as you assign document.URL to something, the page location changes.
Newbie
 
Join Date: Mar 2008
Posts: 8
#3: Mar 30 '08

re: Dynamically change an image based on document.URL


Quote:

Originally Posted by poe

The reason the page gets redirected is because your if statement is making an assignment instead of a comparison.

What you have: blah = blah2

What you need: blah == blah2

With what you have, as soon as you assign document.URL to something, the page location changes.

Thanks, I am very new at JS, so I appreciate you taking the time to answer a simple question.

Chris
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Apr 1 '08

re: Dynamically change an image based on document.URL


Just a note: document.URL is read-only or, at least, should be. IE lets you set it.

For cross-browser functionality, if you ever need to change the URL, use location.href.
Reply