|
I've used some functions, they're alright, but it's very hard to grasp the concept of what to do with them and how to use them. So what are functions in javascript mainly used for? Creating more than one of the same arguments so that it saves space in a code?
|
|
|
|
|
Well, they're similar to variables. But they have more functionality and are not limited to the script. They can be called anywhere in the html document. Most commonly by use of event handelers like onClick, onMouseover, onBlur, etc. Functions without parameters<script type="text/javascript"> <!-- function ask(){ if(prompt("What is 1+1?","")=="2"){ alert("Correct!"); } else{ alert("Wrong!"); } } //--> </script> The above function simply address an if-then statement that askes the user what the sum of 1 and 1 is. If they put in 2, they get the alert "Correct!". If they put in anything else, they get "Wrong!". <a href="#" onClick="ask()">Ask question</a> Here we have incorporated our function's name and parameters into an onClick event. When the user clicks the link, the function is called and executed.
Last Edit: Sept 26, 2005 2:32:39 GMT by Aaron
|
|
|
|
That's part of it.
In PB JS, there's also the use of quickly making it easier for users to edit, otherwise, they'd have to manually edit it, with no grasp of the concept of how too, and its harder to explain. Such as a switch-it or text-to-image
|
|
|
|
|
Your thinking of functions with parameters. That wasn't what I was trying to explain.
|
|
|
|
That's true. Though, both work.
|
|
|
|
|
The only time I use functions is when I want to put a lot of coding into an onClick. It's pretty useful for that.
- Boag
|
|
|
|