Forum Moderators: open

Message Too Old, No Replies

Understanding, Tweaking and Optimizing my JavaScript

         

Jeremy_H

12:48 am on Jan 12, 2006 (gmt 0)

10+ Year Member



I have a script (below) on my site that I'm trying to understand, tweak and optimize.

I have a couple of questions that I'm hoping somebody might be able to help me on.

Is there a reason/benefit of defining the function alpha, and then later calling for the function to run? Or should those steps just take place without the Part1 and Part2 steps?

Also, I have an understanding of if/else commands, but I don't understand how the if stand is used. With those variables on the inside does it make a difference, or can the script be widdled down even more by just calling immediately for the window.onload command?

Thank you so much for any help or advice.

<script type="text/javascript">
function alpha(){
if(document.getElementById&&document.getElementsByTagName){
window.onload=function(){
(document.getElementById('sel1').onchange=function(){beta(1);})();
}
}
}

alpha();
</script>

Jeremy_H

1:27 am on Jan 12, 2006 (gmt 0)

10+ Year Member



OK, I further worked on the code, and it looks like I can get rid of the A,B steps, and the if statement. (The script runs fine, but I hope I haven't broken any coding rules, or did something that made it so it wouldn't work on some platforms--anybody know?).

That part of the script has been shrunken down quite a bit to now just:

<script type="text/javascript">
window.onload=function(){
(document.getElementById('sel1').onchange=function(){beta(1);})();
}
</script>

So let me understand what this script is saying. Once the website has finished being downloaded, then look at id sel1, and if it changes, then run function beta with a value of 1? Is that right, or am I missing something.

Besides removing the line breaks and spaces, does anybody else see any other ways to optimize this piece of the code?

Thanks.

Fotiman

3:23 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your shrunk down version has a problem. In the original, the function first checks to see if the browser supports the methods "getElementById" and "getElementsByTagName". This latter is not really needed since your function isn't trying to use that method. If the browser does not support "getElementById", then when you try to run:

document.getElementById('sel1')

You will get a javascript error. If the browser does not support "getElementById", you method will not work, but better to include the check for is so that at least the user doesn't get a javascript error.

And yes, your understanding of what the code is doing is correct. I don't think you can really optimize it any more than that.

Jeremy_H

7:04 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



Thanks Fotiman for your reply.

So the command below only gets processed IF getElementById AND getElementsByTagName are supported. If not, then nothing would happen, and if so, then the script would process? And if those commands weren't there, then if they were not supported the use would get an error, and if they were, then the script would process?

if(document.getElementById&&document.getElementsByTagName){...}

In what situations would getElementById and getElementsByTagName not be supported? Are there different versions of JavaScript, or are those commands proprietary and only supported in certain browsers? (If the latter is so, is there a more open way to get that information?)

Thanks

Fotiman

8:13 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




So the command below only gets processed IF getElementById AND getElementsByTagName are supported. If not, then nothing would happen, and if so, then the script would process?

Correct.


And if those commands weren't there, then if they were not supported the use would get an error, and if they were, then the script would process?

Correct.


if(document.getElementById&&document.getElementsByTagName){...}

In what situations would getElementById and getElementsByTagName not be supported? Are there different versions of JavaScript, or are those commands proprietary and only supported in certain browsers? (If the latter is so, is there a more open way to get that information?)

There are different versions of the DOM. It depends on which DOM the browser supports. Older browsers (like IE 4) don't support getElementById. IE5+ and Mozilla (Netscape 6+, Firefox) support the DOM that uses getElementById. I don't know about Opera or other browsers.

Jeremy_H

7:55 am on Jan 13, 2006 (gmt 0)

10+ Year Member



Awesome! I think I'm starting to finally get a basic grasp on the language.

Thanks for your help.