Web Analytics Made Easy -
StatCounter Can I append a value declared in HTML to image src in external js? - CodingForum

Announcement

Collapse
No announcement yet.

Can I append a value declared in HTML to image src in external js?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can I append a value declared in HTML to image src in external js?

    I have a var image_server = 'http://imageserver:8004' in my html and then I link to external.js .

    What I want to do within external.js is:

    var image1 = new Image();
    image1.src = $image_server + '/images/myImage1.jpg';

    Will this work? It doesn't seem to be. I am relatively new to JS and would appreciate any pointers, code or tips you could give me.

    Thanks in advance!

  • #2
    It should work if you load the external js first

    <script type="text/javascript" src="external.js"></script>

    <script type="text/javascript">

    var image1 = new Image();
    image1.src = $image_server + '/images/myImage1.jpg';


    </script>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

    Comment


    • #3
      well, that $ before the variable will mess you up. Too much PHP on the brain. :P

      What you're asking is whether or not an external javascript is within the same scope as inline scripts, and the answer is yes. External scripts do not get their own scope, so their base scope is the global scope.

      Comment

      Working...
      X