Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-02-2009, 06:18 PM   PM User | #1
hroyale
New to the CF scene

 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
hroyale is an unknown quantity at this point
Help IE Bug getNamedItem("width")

Hi,
I'm working on a bit of JavaScript that will read the source and other information from an img tag for some CSS manipulation.

So basically I'm using getElementById with a workaround for the IE failings with that method.
var myImage = document.getElementById("myLogo");

Then I grab the attributes by name with:
var imageattributes=myImage.attributes;
var imgURL = imageattributes.getNamedItem("src").value;
var imgHeight = imageattributes.getNamedItem("height");
var imgWidth = imageattributes.getNamedItem("width");
var imgAlt = imageattributes.getNamedItem("alt");

It all works great in IE8 and FireFox.

The alt and src work great in everything.

However IE 6 and 7 (and compatibility view IE8) always return "0" as the value for height and width even when another value is set. They return "0" when the tags aren't included at all.

I did a google search but I couldn't find anything about this specific IE bug, assuming it's a bug and not something I'm overlooking.

Anyone have any tips or similar experiences?

Thanks,
Heather
hroyale is offline   Reply With Quote
Old 10-02-2009, 06:41 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
var imgHeight = imageattributes.getNamedItem("height").value;
var imgWidth = imageattributes.getNamedItem("width").value;
var imgAlt = imageattributes.getNamedItem("alt").value;

Fine in IE6/7 but does not work in Firefox. So you will need a bit of browser detection and alternative code.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Last edited by Philip M; 10-03-2009 at 09:57 AM..
Philip M is offline   Reply With Quote
Old 10-02-2009, 10:30 PM   PM User | #3
hroyale
New to the CF scene

 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
hroyale is an unknown quantity at this point
Thanks. Sorry, that was a typo in my posting. I'm using .value in all instances.

The odd thing is that the alt value and src value work just fine in IE6 and 7, it's just the width and height that are read as "0" no matter what.

So to reiterate, this is my code:

var imageattributes=myImage.attributes;
var imgURL = imageattributes.getNamedItem("src").value;
var imgHeight = imageattributes.getNamedItem("height").value;
var imgWidth = imageattributes.getNamedItem("width").value;
var imgAlt = imageattributes.getNamedItem("alt").value;

imgURL and imgAlt work great in all browsers.

imgHeight and imgWidth always return "0" in IE 6 and IE 7, but return the true value in IE 8, FireFox, Chrome....

Why does "width" behave differently than "src" in IE? Is there a workaround?
hroyale is offline   Reply With Quote
Old 10-03-2009, 09:13 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by hroyale View Post
The odd thing is that the alt value and src value work just fine in IE6 and 7, it's just the width and height that are read as "0" no matter what.

imgHeight and imgWidth always return "0" in IE 6 and IE 7, but return the true value in IE 8, FireFox, Chrome....

Why does "width" behave differently than "src" in IE? Is there a workaround?
This returns the correct width and height (200 and 100 in my test image) in IE6/7:

Code:
<img src = "one.jpg" id = "myLogo" alt="The Logo">

<script type = "text/javascript">

var myImage = document.getElementById("myLogo");

var imageattributes=myImage.attributes;
var imgURL = imageattributes.getNamedItem("src").value;
alert ("URL = " + imgURL);

var imgHeight = imageattributes.getNamedItem("height").value;
alert ("Height " + imgHeight);

var imgWidth = imageattributes.getNamedItem("width").value;
alert ("Width " + imgWidth);

var imgAlt = imageattributes.getNamedItem("alt").value;
alert ("Alt = " + imgAlt);

</script>
Philip M is offline   Reply With Quote
Old 10-03-2009, 11:30 AM   PM User | #5
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,358
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Quote:
This returns the correct width and height (200 and 100 in my test image) in IE6/7:
only if the image has loaded(this may be the problem)
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 10-03-2009, 11:52 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by vwphillips View Post
only if the image has loaded(this may be the problem)
Yes, but then the src and the alt would not show.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
attributes, bug, getelementbyid, img, internetexplorer

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:41 PM.


Advertisement
Log in to turn off these ads.