View Single Post
Old 02-08-2013, 07:59 PM   PM User | #7
boyo1991
New Coder

 
Join Date: Nov 2010
Location: i live online.
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
boyo1991 is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
Jumbling the JavaScript with the HTML like that is a really bad idea - it used to be the only way to write JavaScript back when Netscape was the only browser that supported it but since IE5 there has been no reason whatever to jumble the JavaScript in with the HTML. Since IE5 all JavaScript should go in a separate file.

onload is also unnecessary except if your script needs to confirm if all the images have loaded. For simple interactions with the page simply attaching the script at the bottom of the page results in a lot less code and the script running a lot sooner.

In addition, global variables are almost completely unnecessary in JavaScript - the only time they are needed is to provide a way to reference a library that is in a separate file. For stand alone scripts that are not dependent on a library the only use that global variables have is to possibly clash with other scripts to break the functioning of the page.

Also alert() is for debugging purposes only and should never be used in a published script.

Defining names functions rather than simply assigning anonymous functions to variables also limits your ability to use functions as flexibly as JavaScript allows but with the types of scripts that JavaScript beginners produce this doesn't really matter.

Effectively there is nothing whatsoever in your code that should be used in writing JavaScript for modern browsers - except for beginners using that less flexible way of defining their functions.
well i was not to say that it was the only way... it could be simply done at the bottom of the script as:

main();

and your done..

as for the global variables, i understand, but they still do exist.. so i added them into the example.. and for an alert(), i understand this as well, but this example is not based on what the program does, its about a technique for writing it. so maybe this were for debugging purposes? its just to show whats supposed to go through the app.

i do appreciate the input however!
boyo1991 is offline   Reply With Quote