Connecting Tech Pros Worldwide Help | Site Map

Showwing an image from a variable?

  #1  
Old July 20th, 2005, 01:01 PM
Ross M. Greenberg
Guest
 
Posts: n/a
If I have a variable named 'fred', the contents looking like this:
'/gif/picture1.gif', how can I display that image?

Thanks!

Ross

  #2  
Old July 20th, 2005, 01:01 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a

re: Showwing an image from a variable?


"Ross M. Greenberg" <greenber@catskill.net> writes:
[color=blue]
> If I have a variable named 'fred', the contents looking like this:
> '/gif/picture1.gif', how can I display that image?[/color]

Where?

You can do this:
---
var img = new Image();
img.src=fred;
document.body.appendChild(img);
---
but that displays the image at the end of the document.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #3  
Old July 20th, 2005, 01:01 PM
Richard Cornford
Guest
 
Posts: n/a

re: Showwing an image from a variable?


"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:3cci5uf3.fsf@hotpop.com...
<snip>[color=blue]
> You can do this:
> ---
> var img = new Image();
> img.src=fred;
> document.body.appendChild(img);
> ---
> but that displays the image at the end of the document.[/color]

On some of the more recent browsers new Image() is very like
document.createElement('img') and does return an object with all of the
characteristics of an IMG element. But they are not required to be
equivalent, and I don't think I would recommend new Image() over
createElement (if available, so probably whenever appendChild is
available (except for possible problems with IE 4 and late Opera 6
versions [1])). On IceBrowser, for example, creating and appending an
IMG element will work but the global Image constructor is a functionless
dummy.

Richard.

[1] For reasons that have never become clear Opera 6 (at least the later
versions) has a document.createElement function, but as it is not
possible to dynamically alter the DOM in the browser on Opera 6 there
doesn't seem much point in having the function (and I don't think that
it is functional). IE 4 has both document.createElement and appendChild
methods on its elements but they are pre-W3C DOM methods and cannot be
used in the same way as the W3C versions.


  #4  
Old July 20th, 2005, 01:01 PM
Ross M. Greenberg
Guest
 
Posts: n/a

re: Showwing an image from a variable?


The variable "fred" is defined in the HEAD, and as such is static for the life
of the rendering.

I want a tag such as <IMG src=fred...>, but I simply don't know the proper
syntax for such.

Newbie question I know, but then I are one...

Thanks!

Ross
"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:3cci5uf3.fsf@hotpop.com...[color=blue]
> "Ross M. Greenberg" <greenber@catskill.net> writes:
>[color=green]
> > If I have a variable named 'fred', the contents looking like this:
> > '/gif/picture1.gif', how can I display that image?[/color]
>
> Where?
>
> You can do this:
> ---
> var img = new Image();
> img.src=fred;
> document.body.appendChild(img);
> ---
> but that displays the image at the end of the document.
>
> /L
> --
> Lasse Reichstein Nielsen - lrn@hotpop.com
> DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
> 'Faith without judgement merely degrades the spirit divine.'[/color]

  #5  
Old July 20th, 2005, 01:01 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a

re: Showwing an image from a variable?


"Ross M. Greenberg" <greenber@catskill.net> writes:

Please don't top-post.
[color=blue]
> The variable "fred" is defined in the HEAD, and as such is static
> for the life of the rendering.
>
> I want a tag such as <IMG src=fred...>, but I simply don't know the proper
> syntax for such.[/color]

Where do you want it?

What you can do is to add
<script type="text/javascript">
document.write("<img src=\""+fred+"\" ... >");
</script>

That will add an image tag where the script tag is.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #6  
Old July 20th, 2005, 01:01 PM
Grant Wagner
Guest
 
Posts: n/a

re: Showwing an image from a variable?


<head>
<script type="javascript">
var fred = 'path/to/image.jpg';
</script>
</head>
<body>
<script type="text/javascript">
document.write('<img src="' + fred + '" />');
</script>
</body>

Of course if the user doesn't have client-side JavaScript enabled, they get
nothing. I better alternative might be:

<head>
<script type="javascript">
var fred = 'path/to/image.jpg';
</script>
</head>
<body onload="
if (document.images && document.images['myImage']) {
document.images['myImage'].src = fred;
}
">
<img name="myImage" src="/path/to/dummyImage.jpg" />
</body>


"Ross M. Greenberg" wrote:
[color=blue]
> The variable "fred" is defined in the HEAD, and as such is static for the life
> of the rendering.
>
> I want a tag such as <IMG src=fred...>, but I simply don't know the proper
> syntax for such.
>
> Newbie question I know, but then I are one...
>
> Thanks!
>
> Ross
> "Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
> news:3cci5uf3.fsf@hotpop.com...[color=green]
> > "Ross M. Greenberg" <greenber@catskill.net> writes:
> >[color=darkred]
> > > If I have a variable named 'fred', the contents looking like this:
> > > '/gif/picture1.gif', how can I display that image?[/color]
> >
> > Where?
> >
> > You can do this:
> > ---
> > var img = new Image();
> > img.src=fred;
> > document.body.appendChild(img);
> > ---
> > but that displays the image at the end of the document.
> >
> > /L
> > --
> > Lasse Reichstein Nielsen - lrn@hotpop.com
> > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
> > 'Faith without judgement merely degrades the spirit divine.'[/color][/color]

--
| Grant Wagner <gwagner@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


  #7  
Old July 20th, 2005, 01:02 PM
Fabian
Guest
 
Posts: n/a

re: Showwing an image from a variable?


Lasse Reichstein Nielsen hu kiteb:
[color=blue]
> "Ross M. Greenberg" <greenber@catskill.net> writes:
>
> Please don't top-post.
>[color=green]
>> The variable "fred" is defined in the HEAD, and as such is static
>> for the life of the rendering.
>>
>> I want a tag such as <IMG src=fred...>, but I simply don't know the
>> proper syntax for such.[/color]
>
> Where do you want it?
>
> What you can do is to add
> <script type="text/javascript">
> document.write("<img src=\""+fred+"\" ... >");
> </script>
>
> That will add an image tag where the script tag is.[/color]

Yet another methodf is:

document.getElementById(id).src = fred;

Where you have an ID attribute on the image.

--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing pictures and playing MIDI files larystoy answers 1 November 29th, 2007 04:40 AM