JavaScript and client side scripting.
Moderator: General Moderators
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Fri Jul 27, 2007 1:23 am
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 ??
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Fri Jul 27, 2007 1:31 am
Close..
Code: Select all
<script>
var location = 'Some locations';
document.write('<img src="'+location+'" />');
</script>
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Fri Jul 27, 2007 1:46 am
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 .
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Fri Jul 27, 2007 3:00 am
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 » Fri Jul 27, 2007 4:41 am
OK Thanks I'll try both.