Forum Moderators: open
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>
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.
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.
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
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.