473,398 Members | 2,427 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,398 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 30607
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.