Connecting Tech Pros Worldwide Help | Site Map

Showwing an image from a variable?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 12:01 PM
Ross M. Greenberg
Guest
 
Posts: n/a
Default Showwing an image from a variable?

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, 12:01 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default 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, 12:01 PM
Richard Cornford
Guest
 
Posts: n/a
Default 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, 12:01 PM
Ross M. Greenberg
Guest
 
Posts: n/a
Default 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, 12:01 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default 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, 12:01 PM
Grant Wagner
Guest
 
Posts: n/a
Default 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, 12:02 PM
Fabian
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.