<img src="var">

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

<img src="var">

Post 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 ??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: <img src="var">

Post by John Cartwright »

Close..

Code: Select all

<script>
var location = 'Some locations';
document.write('<img src="'+location+'" />');
</script>
:wink:
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post 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 .
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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.
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

OK Thanks I'll try both.
Post Reply