PDA

View Full Version : Can I append a value declared in HTML to image src in external js?


bermanbp
09-27-2006, 05:01 PM
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!

Mr J
09-27-2006, 07:31 PM
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>

Beagle
09-28-2006, 02:47 PM
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.