Connecting Tech Pros Worldwide Help | Site Map

Dynamically change an image based on document.URL

  #1  
Old March 28th, 2008, 10:21 PM
Newbie
 
Join Date: Mar 2008
Posts: 8
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 12:46 PM. Reason: Added code tags
  #2  
Old March 29th, 2008, 02:57 AM
poe poe is offline
Member
 
Join Date: Jun 2007
Location: Tennessee
Posts: 32

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

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
  #4  
Old April 1st, 2008, 12:47 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,525
Provided Answers: 12

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
.Net frameword Resources ( vb.net , asp.net etc...) shamirza answers 0 January 17th, 2007 08:05 AM
Timer Component for VB 2005 .aspx page? Ryan answers 10 June 9th, 2006 08:25 AM
Setting styles dynamically only for @media screen C A Upsdell answers 1 July 21st, 2005 12:28 AM
value is null or not an object, but its defined? cwizard answers 16 July 20th, 2005 02:27 PM