473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Jul 20 '05 #1
6 30606
"Ross M. Greenberg" <gr******@catskill.net> writes:
If I have a variable named 'fred', the contents looking like this:
'/gif/picture1.gif', how can I display that image?


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:3c**********@hotpop.com...
<snip>
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.


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.
Jul 20 '05 #3
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" <lr*@hotpop.com> wrote in message
news:3c**********@hotpop.com...
"Ross M. Greenberg" <gr******@catskill.net> writes:
If I have a variable named 'fred', the contents looking like this:
'/gif/picture1.gif', how can I display that image?


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Jul 20 '05 #4
"Ross M. Greenberg" <gr******@catskill.net> writes:

Please don't top-post.
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.


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
<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:
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" <lr*@hotpop.com> wrote in message
news:3c**********@hotpop.com...
"Ross M. Greenberg" <gr******@catskill.net> writes:
If I have a variable named 'fred', the contents looking like this:
'/gif/picture1.gif', how can I display that image?


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


--
| Grant Wagner <gw*****@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
Jul 20 '05 #6
Lasse Reichstein Nielsen hu kiteb:
"Ross M. Greenberg" <gr******@catskill.net> writes:

Please don't top-post.
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.


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.


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
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Shapper | last post by:
Hello, I am declaring a variable in my aspx.vb code as follows: Public Class catalogue Public productid As String Private Sub Page_Load ... I have an image button where I call the...
3
by: jcrouse | last post by:
How do you empty the contents of a variable the contains an image? Thank you, John
1
by: Tufty | last post by:
Hi, I have a webpage that lets the user upload an image. This is the posted to a perl script as a form var. The data is stored in SQL 2005 (as the new image object). I now need to get it back...
3
by: Peter | last post by:
..NET 2.0 I have an Image on my form and I am trying to clear this image and re-load it with different image. Everything works fine, but once I load the image I can not delete this image until...
1
by: spgedwards | last post by:
I am trying to run a basic script that displays an existing jpeg image and writes some text over it. Sounds simple, but I cannot seem to be able to colour the font correctly. In the example below...
2
by: Bjorn Sagbakken | last post by:
Hi. This story is about uploading jpg's, then resize them to fixed width or height and storing them to an SQL table. The only way I have found so far is to read the uploaded file to an...
1
by: jzieba | last post by:
Can someone tell me how to initialize the variable "display_image" below? The following code returns a bad source when it is first loaded. If I check properties on the image it states "Not...
5
by: Jeff | last post by:
hi asp.net 2.0 I get this compile error: 'Image' does not contain a definition for 'ImageUrl' Image image = (Image)e.Item.FindControl("img"); image.ImageUrl = "~/image.png";
6
JOHNYKUTTY
by: JOHNYKUTTY | last post by:
i have to convert an image variable(system.drawing.bitmap orsystem.drawing.image or AForge.Imaging.Image) to Emgu.CV.Image type i have used the code below Image<Bgr, Byte> cvimage = new Image<Bgr,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.