Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Dynamically change an image based on document.URL

Question posted by: ccountey (Newbie) on March 28th, 2008 09:21 PM
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.  
-->

</script>





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



adWords();



</script>


Thanks!
poe's Avatar
poe
Newbie
31 Posts
March 29th, 2008
01:57 AM
#2

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.

Reply
ccountey's Avatar
ccountey
Newbie
8 Posts
March 30th, 2008
01:28 AM
#3

Re: Dynamically change an image based on document.URL
Quote:
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

Reply
acoder's Avatar
acoder
Site Moderator
11,501 Posts
April 1st, 2008
11:47 AM
#4

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
Reply
Not the answer you were looking for? Post your question . . .
189,071 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Javascript / DHTML / Ajax Forum Contributors