Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Team Studio

Team Studio Avatar

******
Administrator

120


June 2005
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? ???


Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
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

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
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

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
Your thinking of functions with parameters. That wasn't what I was trying to explain.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
That's true. Though, both work.

Boag

Boag Avatar
Yo Yo Ma!

**
Official Member

65


September 2005
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

Moose

Moose Avatar

****
Senior Member

449


August 2005
CD, there is no such thing as PB JS. :P
Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

newBookmarkLockedFalling