This has everything to do with scope. All Javascript elements are parsed in sequence. First the ones in the head and then the ones in the body. Functions are parsed first and aren't executed when they are defined. Declarations and function calls are executed afterwards. example:
<script>
runCode();
function runCode()
{
alert(1);
}
</script>
Will work, since the function runCode is defined first while parsing, however this example will fail:
<script>
runCode();
</script>
<script>
function runCode()
{
alert(1);
}
</script>
This will fail, runCode
is called before it's defined, since the second script block isn't parsed yet. The following example will work:
<script>
function runCode()
{
runUpdate()
}
</script>
<script>
function runUpdate()
{
alert(1);
}
runCode();
</script>
Even though runUpdate
isn't defined when runCode
is parsed it will not raise an undefined error since the content of the function isn't executed until called upon.
So at the end of the document loading, all the Javascript is parsed into a global scope. (Or simplified: it's put onto one big pile).
So when the document is loaded the code looks like this:
function validate(){
//validate body
//how to call webService() here;
}
function webService(){
//WEB SERVICE FUNCTION BODY
}
and your input with click event can call upon validate()
and validate
can call upon webservice
because there both defined.