Calling Functions

Sometimes scripts can be rather irritating to some people. I've gone to pages that had so so many pop-ups that I wanted to back out of the page. This is hard to do and has shut off my webtv on alot of occasions. So I would make your scripts short on your main page so not to chase off visitors. Also you could put buttons on your page to start different scripts. The only problem I have with this style of script is that I havn't figured out if it is possible to use the document.writeln("contents") command. I don't think it is possible because the page has already been written. But you can use it for prompts and alerts.
Here are some samples
Another good thing about this is you don't have to reload the page to try the script again. You can just click on it to do the script again.
Try Sample C again and make your age under 5 or over 90 for a different response.

If you create one that only works once and then can't be ran again it means there's a mistake in the script, not one bad enough to keep it from running once but bad enough to keep any more scripts from running again unless the page is reloaded. I know this because while writing the scripts to be called I made a mistake that I didn't notice and after testing the page I noticed the scripts only ran once. I looked over my scripts and found a simple mistake. Now that I've got that fixed they will run over and over again.

What you are doing when you do this is making a JavaScript function. Instead of just writing a script up in the head of the page you write a function up in the head of the page.

The function is called using a form wich is what we went over on the last page.

. Here is a sample function and form to call the function

<script language="JavaScript">
<!-- Hide
function sample() { alert("Welcome to my page";)}
// End Hide -->
</script>
That is a function that will make an alert. It's just like writing a normal script except for writing it like this
function sample(){ Your script goes in here }
You also give the function a name. I named this one sample. The () goes after every function name. More complex function take variable from the forms for use in the function. The function I'm teaching you now don't accept variable from the forms so it's just () after the name. This doesn't mean that you can't use variables in the function it just means that no variable will come from the form.

You can place multiple functions up in the head of your page, and then call them by different buttons located on your page.

. Here is how you make the button to call it up

<form>
<input type="button" name="button" value="your writing here" onClick=sample()>
</form> That is actually quite simple. In the value, where I have your writing here, place what ever you want the button to say on it. Like click here or anything. I haven't tried writing something really long on it, I'm not sure what would happen, maybe a really big button.
In the space where I have written sample() is the place where you put the name of your function to be called at.

Now lets say you have multiple functions at the and you want the button side by side like I have at the top of this page. When ever you start a new form you get a line break. You don't have to start a new form for each button.

<center>
<form>
<input type="button" name="button" value="function 1" onClick=sampleA()>
<input type="button" name="button" value="function 2" onClick=sampleB()>
</form>
<center>

I have added two more functions to my page. sampleA() and sampleB() wich brings the total to 5. Remember a mistake will cause the rest to stop working too. So it is best to add your functions one at a time and test after each addition. If you write a bunch of functions and then test it you may not know where your error is. this may be different on a computer but since I am on webtv that is how it works for me.

Also the variables from one function can be used in the others. If a prompt came up and asked for a name you could use the variable again later in a different script so you wouldn't have to ask the questions again. But if the question hadn't been asked yet and you tried to use it the script would fail and may cause the rest of your scripts to fail.

I will add more to this as I learn more. Or find mistakes I have made on this page. Remeber if you need help there is a message board at the bottom of page 1

The next page will be on making password so only invited people can view your page.


Passwords


|| index||page 1||page 2||page 3||page 4||page 5||page 6||page 7||Page 8||
||glossary||library||Links||Labratory||Awards||