473,386 Members | 1,841 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.

Unsetting an IMG height/width

I have an IMG placeholder for what will normally be a tiny (6x7) img.
Sometimes however, I will use a somewhat larger but still small image.
How can I unset the dimensions for the <IMG id=normallyTiny ...>
element I have already specified, rather than using the tortured code
below? Unfortunately, I must set the dimensions at the outset, because
otherwise the placeholder for the 'normallyTiny' image will be too big.

<BODY onload="docLoaded()">
<IMG id=dummyImg onload="reviseImg()" style="visibility:hidden"
src="http://us.i1.yimg.com/us.yimg.com/i/us/pim/f/perc1.gif">
<IMG height=6 width=7 id=normallyTiny>

<SCRIPT type='text/javascript'>
function docLoaded() {
alert('document is loaded: ' + typeof(document.body.onload)); }
function reviseImg() {
alert('dummy image is preloaded');
var dummyImg = document.getElementById('dummyImg');
var img = document.getElementById('normallyTiny');
img.width = dummyImg.width;
img.height = dummyImg.height;
img.src = dummyImg.src; }
</SCRIPT>
</BODY>
Trying delete img.width has no effect and trying img.width=0 or "" or
null merely cause the subsequent image to not show.

Note that the order of the alerts differs between IE6 and FF Deer Park
Alpha.

An unrelated question is: where is the document's onload function
living in Firefox? The code above (see the 'typeof') finds it for IE.

Thanks,
Csaba Gabor from Vienna

Jul 24 '05 #1
6 9190


Csaba Gabor wrote:
I have an IMG placeholder for what will normally be a tiny (6x7) img.
Sometimes however, I will use a somewhat larger but still small image.
How can I unset the dimensions for the <IMG id=normallyTiny ...>
element I have already specified, rather than using the tortured code
below? Unfortunately, I must set the dimensions at the outset, because
otherwise the placeholder for the 'normallyTiny' image will be too big.


That is difficult to achieve I think as browsers behave differently, if
you only care about modern browsers then the best approach is to create
a new image element e.g.
var img = document.createElement('img');
// set src as needed
img.src = 'whatever.gif';
and then replace the other image element e.g.
var oldImg = document.getElementById('normallyTiny');
if (oldImg && oldImg.parentNode && oldImg.parentNode.replaceChild) {
oldImg.parentNode.replaceChild(img);
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 24 '05 #2
Csaba Gabor said the following on 7/24/2005 11:52 AM:

<snip>
Note that the order of the alerts differs between IE6 and FF Deer Park
Alpha.
I get the same order of alerts in IE6 and Firefox 1.0.4
An unrelated question is: where is the document's onload function
living in Firefox? The code above (see the 'typeof') finds it for IE.


In the window object (where it should be). That is why you see code such as:

window.onload = someFunction;

rather than:
document.onload = someFunction;

Change your typeof test to test for window.onload instead of
document.body.onload and you will see the alert then.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 24 '05 #3
Martin Honnen wrote:
Csaba Gabor wrote:
I have an IMG placeholder for what will normally be a tiny (6x7) img.
Sometimes however, I will use a somewhat larger but still small image.
How can I unset the dimensions for the <IMG id=normallyTiny ...>
element I have already specified, rather than using the tortured code


That is difficult to achieve I think as browsers behave differently, if
you only care about modern browsers then the best approach is to create
a new image element e.g.


<BODY onload="docLoaded()">
<DIV>
<IMG height=6 width=7 id=normallyTiny
src="http://us.i1.yimg.com/us.yimg.com/i/us/pim/f/perc1.gif">
</DIV>
<SCRIPT type='text/javascript'>
function docLoaded() {
alert('document is preloaded');
var newImg = document.createElement('IMG');
newImg.id="normallyTiny";
newImg.src="http://us.i1.yimg.com/us.yimg.com/i/us/pim/f/pero1.gif";
var oldImg = document.getElementById('normallyTiny');
var outImg = oldImg.parentNode.replaceChild (newImg, oldImg);
alert(document.getElementById('normallyTiny').pare ntNode.innerHTML);
// document.body.appendChild(outImg);
newImg.parentNode.parentNode.insertBefore(outImg, newImg.parentNode);
alert(document.getElementById('normallyTiny').pare ntNode.innerHTML);
}
</SCRIPT>
</BODY>

Thanks very much Martin. Given that I can't unset the dimensions, your
method is far cleaner than mine.

I'll submit a second more proper example, but this code brings up an
enrelated issue of id's, which are supposed to be unique. I've
deliberately reintroduced the old image to duplicate the ids and see
what the last alert reports. It shows a distinction between the two
browsers: With FF, it always seems to be the most recently inserted Id
that 'wins'. In the case of IE, it seems to be the one occurring first
in document order (swap the line commented out with the one after it to
observe the change). The moral of the story is, it's the user's
responsibility to ensure unique ids.

Csaba Gabor from Vienna

Jul 25 '05 #4
This second example illustrates how I would really adapt Martin's
example. The first example was just for the purposes of exploring what
happened when the browser was presented with multiple elements with the
same id. In the example below, I am intending to reuse the one
imported image multiple times, hence the use of .cloneNode

<BODY onload="docLoaded()">
<DIV id=imgDiv>
<IMG height=6 width=7 id=img1
src="http://us.i1.yimg.com/us.yimg.com/i/us/pim/f/perc1.gif">
<IMG height=6 width=7 id=img2
src="http://us.i1.yimg.com/us.yimg.com/i/us/pim/f/perc1.gif">
</DIV>
<SCRIPT type='text/javascript'>
function docLoaded() {
var ni, newImg = document.createElement('IMG');
newImg.src="http://us.i1.yimg.com/us.yimg.com/i/us/pim/f/pero1.gif";
var oldImg = document.getElementById('img1');
var parDiv = oldImg.parentNode;
parDiv.replaceChild (ni=newImg.cloneNode(false), oldImg);
ni.id = oldImg.id;
oldImg = document.getElementById('img2');
parDiv.replaceChild (ni=newImg.cloneNode(false), oldImg);
ni.id = oldImg.id;
alert(document.getElementById('imgDiv').innerHTML) ;
}
</SCRIPT>
</BODY>
Csaba Gabor from Vienna

Jul 25 '05 #5
Randy Webb wrote:
Csaba Gabor said the following on 7/24/2005 11:52 AM:
An unrelated question is: where is the document's onload function
living in Firefox? The code above (see the 'typeof') finds it for IE.


In the window object (where it should be). That is why you see code such as:
window.onload = someFunction;


Thanks Randy, that was quite helpful. I was especially pleased that
both my questions were answered.

But perhaps you can expound on that "where it should be" comment.
There is a 1 to 1 correspondence between the window and the
document.body. Given that all the other onloads (for example image
elements) have the .onload sitting on a DOM element, having the onload
for the document (body) sitting on the window object seems strangely
incongruous. I am curious to know the reasoning invoved.

Thanks,
Csaba Gabor from Vienna

Jul 25 '05 #6
img.removeAttribute("width");
May 10 '06 #7

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

Similar topics

10
by: John | last post by:
I have a table with two rows. On the first row is a text box and in the second row is an image. I have set the table cellpadding to 0 and cellspacing to 0. The table is leaving extra spaces in the...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
1
by: polocar | last post by:
Ciao a tutti, leggendo qua e lą per il forum ho scoperto che non sono l'unico ad avere questo problema. Se si inserisce un controllo ComboBox in un form di C#, non č possibile impostare la sua...
1
by: comp.lang.php | last post by:
Sorry folks, I'm on a roll!!! I need to calculate the height or width of an image if you change either the width or height and don't change BOTH of them at the same time, this is to "vector" the...
4
by: reycri | last post by:
I have a page that works as I intend in IE but not in Firefox: <html> <head> <title>Overflow Test</title> </head> <body style='overflow:hidden; margin:0; padding:0;'> <table border='0'...
8
by: Kentor | last post by:
Hello, I have users that submit images to my website. By default I choose to make the width bigger than the height because usually pictures are taken horizontally... But in some cases people take...
1
by: mascouta | last post by:
I have a lot of problems with IE browser, one of them is described in this topic. in my website i have div Calculator with background image. it displayed perfectly with Firefox browser however in IE...
1
by: pravinnweb | last post by:
can anyone tell me how to set auto height to outer div that is in green box id "gray-background" it should increase relatively to inner div "smbox" here is the css and html code it should work in...
4
by: AAaron123 | last post by:
<body runat="server" id="MainBody"> <form id="form1" runat="server" style="background-color:green; width: 100%; height: 100%"> <br /> Table1" runat="server" Style="background-color:Yellow;...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.