Connecting Tech Pros Worldwide Help | Site Map

Dynamically change an image based on document.URL

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 28th, 2008, 09:21 PM
Newbie
 
Join Date: Mar 2008
Posts: 8
Default Dynamically change an image based on document.URL

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!

Last edited by acoder; April 1st, 2008 at 11:46 AM. Reason: Added code tags
Reply
  #2  
Old March 29th, 2008, 01:57 AM
poe poe is offline
Member
 
Join Date: Jun 2007
Location: Tennessee
Posts: 32
Default

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
  #3  
Old March 30th, 2008, 01:28 AM
Newbie
 
Join Date: Mar 2008
Posts: 8
Default

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
Reply
  #4  
Old April 1st, 2008, 11:47 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,227
Default

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.