[Javascript] Code optimization

Paul Novitski paul at juniperwebcraft.com
Mon Aug 7 17:15:25 CDT 2006


At 07:05 AM 8/7/2006, Terry Riegel wrote:
>I need a little help optimizing the following code.
>
><div class="nor" id="004LukeShannon.jpg">
>  <A href="javascript:windowHandle=window.open('http:// 
> clearimageonline.com/pages/htmlos/94150.128.040923622659991927',
>'CIclearimage004LukeShannonjpg',
>'width=550,height=550,status=yes,scrollbars=yes,resizable=yes,left=150') 
>;windowHandle.focus();" >
>   <img src="/clearimage/images/pict-small.gif" border="0" width="12"
>height="12" align="left">
>  </A>
>  <a href="javascript:changedest('004LukeShannon.jpg','FILE')"
>ondblclick="changedest2('004LukeShannon.jpg','FILE')">
>   004LukeShannon.jpg
>  </a>
></div>
>
>
>What I would like to do is replace some of the references in the
>second <a> tag with a relative reference to the containing div. I
>would also like to replace all of the links on the page with similar
>code, I think there is a way to do this in a script that will apply
>to all links globally for that page. I would just need to know the
>second argument in my code either "FILE" or "DIR"


You might be looking for the parentNode property; see 
http://developer.mozilla.org/en/docs/Gecko_DOM_Reference

What I'd do, however, would be to write a loop that cycled through 
parent nodes and examined child nodes, rather than starting at the 
child level and looking up.

More generally, I recommend:

- Export your javascript to an external file.  Don't use inline 
javascript at all.

- Locate your target elements by id, by class name, by parentage & 
tag name, etc., and apply the desired behaviors to all applicable elements.

- Google "unobtrusive javascript" and read up on some great 
techniques and practices.

Doing these things will let you design your software in a more 
abstract way and makes your scripting more independent of your 
markup.  It also makes it easy to write scripts that fail gracefully 
when javascript is not enabled or expected page elements are not found.

Paul 




More information about the Javascript mailing list