Page 1 of 1

<img src="var">

Posted: Fri Jul 27, 2007 1:23 am
by nwp
What I am trying to do is <img src = a Javascript var and that var will contain that Image location.

Code: Select all

<script>
var = "Some locations";
</script>
<img src="var">
But its not working . How can I do something like this ??

Re: <img src="var">

Posted: Fri Jul 27, 2007 1:31 am
by John Cartwright
Close..

Code: Select all

<script>
var location = 'Some locations';
document.write('<img src="'+location+'" />');
</script>
:wink:

Posted: Fri Jul 27, 2007 1:46 am
by nwp
document.write is not good it rewrites teh whole document and puts only the Image. If we wanna do it in this way we need to either write / append it in a form or a div But thats very complex . So is there any way to do this ? I've also tried

Code: Select all

<script> 
var = "Some locations"; 
</script> 
<img src="Javascript:var">
But it didn't work .

Posted: Fri Jul 27, 2007 3:00 am
by Gente
What about this:

Code: Select all

<html>
<head>
<script type='text/javascript'>
	var Img = "cake.jpg";
</script>
</head>
<body>
<img id="myimg" src="" />
<script type='text/javascript'>
	document.getElementById('myimg').src = Img;
</script>
</body>
</html>
document.write is not good it rewrites teh whole document and puts only the Image
You can write

Code: Select all

<script type='text/javascript'>
	document.write('<img src="'+YourVar+'" />');
</script>
And everything would be ok. I don't see the problem here.

Posted: Fri Jul 27, 2007 4:41 am
by nwp
OK Thanks I'll try both.