Andrea Silvestri
Andrea Silvestri

Reputation: 1134

Javascript IE6 getElementsByTagName not working

I've got this example page where I'm trying to put the wmode of every youtube element inside the page to "transparent".

To do it I need to get all the "object > params" and "embed" tags in the page. The page I link you works like charm in all the browser except for IE6 (haven't cheked the other IEs yet). With IE6 I can't catch the "params" since document.getElementsByTagName('param') returns an empty object, but this doesn't happen with the "embed"! It won't work with document.getElementsByTagName('object') as well

Here is the page http://dl.dropbox.com/u/4064417/provaJs.html

Any suggestion why it's not returning just the "param" tags?

Thank you in advance!

Upvotes: 1

Views: 2187

Answers (3)

Spudley
Spudley

Reputation: 168685

If you must support browsers as old IE6, I would strongly recommend using a library such as jQuery rather than trying to access the DOM directly.

IE6 has a massive number of bugs and quirks that can break even the simplest and most innocuous of Javascript code. jQuery goes to great lengths to abstract these browser bugs and deficiencies away from the developer, allowing you to write code that works on all browsers.

For example rather than using document.getElementsByTagName('object'), you can use the jQuery code $('object'), which will give you the same end result, but will work around any bugs in whichever browser your end user is running.

jQuery does a whole lot more than just hide the browser bugs, of course, but if you're working with IE6, that alone is a good enough reason to use it.

(other libraries are of course available if you really don't like jQuery for some reason)

Upvotes: 0

Daniel Szabo
Daniel Szabo

Reputation: 7281

I understand that you would like to keep your app backwards compatible, but this exercise runs counter-intuitive to the general direction of the industry and its efforts to put IE6 down to rest (even google has dropped support for IE6). Your time might be better invested if you focus on building a better web experience for pseudo-modern browsers instead of worrying about the legacy ones.

Food for thought - maybe use a browser detection script and encourage your users to upgrade so that they can experience your site in all of its modern glory :)

All that being said, ZER0's suggestion is dead-on. Find the "object" tags in the page, and then iterate through its children until you find the tag you're looking for. If you can't seem to grab the object tags with getElementsByTagName, you may very well have to iterate through the document.body.childNodes nodelist, checking for the typeof(N) or N.NodeName along the way.

Upvotes: 1

ZER0
ZER0

Reputation: 25322

I recall some issue with <param/> tags and getElementsByTagName, but it's ages that I don't code in IE6. Instead of querying from document, trying to get the params calling getElementsByTagName from the <object /> itself and see if it helps:

var objects = document.getElementsByTagName("object");

for (var i = 0, object; object = objects[i++];) {
    var params = object.getElementsByTagName("param");

    alert(params.length);
}

Upvotes: 1

Related Questions