|
HTML Tutorial: JavaScript |
|
In this topic, I'll introduce a couple JavaScript tools that you can use without learning a whole programming language. You'll also become savvy enough to use the thousands of JavaScript functions that you can find on the Internet.
To call the alert function, you just write:
alert( 'Any character string you want.' )
Spacing and format are yours to choose. Single or double quotes are fine. Use a backslash followed by the letter "n" where you want a new line. Use a plus sign to combine multiple strings into one. For example:
alert(
"Here's a poem\n"+
'About HTML\n\n'+
'Learn it now\n'+
'And it will serve you well'
)
<form>
<center> <input
name='buttonTickle'
onclick='JavaScript:alert("Hee, Hee! That tickles!")'
type='button'
value='Tickle Me'
> </center>
</form>
onclick event, very sensibly, runs its JavaScript when a button is clicked. For the poem, I wrote a JavaScript function (how to coming next) so I could call it from anywhere.
This is the HTML that creates the Poem button:
<form>
<center> <input
name='buttonPoem'
onclick='Javascript:poem()'
type='button'
value=' Poem, Please '
> </center>
</form>
href="Javascript:whatever_func()" ... in a link. Try this one:
| Poem, Please |
This is the HTML:
<table align=center border=5 cellpadding=3> <tr> <td>
<a href="JavaScript:poem()"> Poem, Please </a>
</td> </tr> </table>
<script>) and end script (</script>) tags. This is the script that creates the poem() function:
<script type='text/JavaScript'>
function poem() {
alert(
"Here's a poem\n"+
'About HTML\n\n'+
'Learn it now\n'+
'And it will serve you well'
)
}
</script>
alert() method has a parameter that receives the text you pass it. You can also write functions that take one or more parameters. You can also search the Internet for functions that someone else has written and is willing to share.
Here's the HTML surrounding my mugshot in the banner of this site:
<a href="javascript:popup( 'html-tutorial-email-martin', 450, 300 )">
<img
alt="icon-sized snapshot of Martin Rinehart"
border=0
style='height:80' width='82'
src="graphics/martin-82-80.jpg"
>
</a>
You are hereby given permission to copy my popup function and use it in your own site. It's in the head section of the banner page (mouse over banner, right click This Frame/View Frame Source). Copy it, including the script tags, and paste it into the head section of one of your pages.
Assuming your explain.html will fit in a 300px by 200px popup window, use this tag to pop up your explanatory page when requested:
<a href='JavaScript:popup( "explain", 300, 200 )'> Explain, please </a>
Got it? Pretty simple stuff if you're not intimidated by the JavaScript part. Now go ask Google where you can find some JavaScript functions ready to use. They're all over the web and many are free.
The final topic on the navigation bar is Web Hosting. Go there when your site is looking good enough to make its debut in cyberspace. Do send me a link.
| Explain the tricky bits! |