W3C home > Mailing lists > Public > www-dom@w3.org > July to September 2008

Re: Using DOM to replace media attribute in the link tag on page load

From: Patrick Garies <pgaries@fastmail.us>
Date: Tue, 22 Jul 2008 04:42:08 -0500
Message-ID: <4885AB70.4090601@fastmail.us>
To: nimblehost <jhead@hawaii.edu>
CC: www-dom@w3.org

nimblehost wrote:
>  The code I've come up with is as follows:
>
>  <script type="text/javascript"> var mediaType =
>  document.getElementsByTagName("link"); for(x=0; x<mediaType.length;
>  x++) { mediaType.item(x).setAttribute("media", "all"); } </script>
>
>  Using firebug to check the current tree of nodes shows that the media
>  attribute remains unchanged. I'm at a loss at how to fix this
>  problem. Any and all help is greatly appreciated.

I just tested this in Mozilla Firefox 3.0.1 by changing a |link| 
element’s |media| attribute value from |print| to |all| and found no 
issues. I checked Firebug 1.2.0b6 and noticed that the DOM had been 
modified as expected.

I don’t see any issues in your shown code assuming that you’re sending 
your XHTML via the MIME type text/html. The problem must be elsewhere in 
your code. You may want to verify that the script is executed /after/ 
your |link| element is created in the DOM.

Your code could use some improvements nevertheless:
* In XHTML served via an XML MIME type, the |getElementsByTagNameNS| 
method should be used instead of |getElementsByTagName|.
* You should declare your variables (i.e., |var x = 0|).
* The MIME type text/javascript is deprecated in favor of the MIME types 
application/javascript and application/ecmascript. Unfortunately, 
neither are supported in Windows Internet Explorer up to version 7.

— Patrick Garies
Received on Tuesday, 22 July 2008 09:42:54 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:59 GMT