[Javascript] Code optimization
Terry Riegel
riegel at clearimageonline.com
Mon Aug 7 17:52:22 CDT 2006
Paul,
I appreciate your help and the general guidelines you are presenting.
I am not trying to ignore them or disregard your advise. I am just
not at that level in my Javascript programming. Actually moving my
Javascript out of my HTML was part of my original question. I thought
I would simplify my question as an aid to my learning, however the
only solution I received was one I had already found via google. That
being parentNode. However this returns UNDEFINED. Here is the exact
code I am referring to. If you would like the larger context, I am
building a web based file manager, and my HTML is being
programmatically generated.
<html>
<div class="nor" id="otherstuff">
<a href="#" >Test</A>
<a href="javascript:alert(this.parentNode)" class="DIR">otherstuff</a>
</div>
</html>
I would be very interested in how to convert the above snip to not
use inline scripting. btw. I prefer inline scripting when testing as
I dont have to worry about external files being cached and wondering
why my code changes are not working. However once I get things
working I dont mind moving everything to an external library.
Thanks,
Terry Riegel
On Aug 7, 2006, at 6:15 PM, Paul Novitski wrote:
> 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=15
>> 0') ;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
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
More information about the Javascript
mailing list