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

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 UTC