Dynamically change an image based on document.URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccountey
    New Member
    • Mar 2008
    • 8

    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”>
    [code=javascript]


    function adWords()



    {



    source=document .URL <!-- If I eliminate this declaration and just use document.URL in the IF statement, the page gets redirected to that URL



    if(source="http ://www.appletreean swers.com/index.html") {



    document.write( "<IMG SRC='/normalimage.jpg '>")



    }else{



    document.write( "<IMG SRC='/adwordimage.jpg '>")



    }



    }



    [/CODE]-->

    </script>





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



    adWords();



    </script>


    Thanks!
    Last edited by acoder; Apr 1 '08, 11:46 AM. Reason: Added code tags
  • poe
    New Member
    • Jun 2007
    • 32

    #2
    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.

    Comment

    • ccountey
      New Member
      • Mar 2008
      • 8

      #3
      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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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.

        Comment

        Working...