To be able to program in javascript on your web page, you can do a couple of different things. One, you can include a javascript source reference in your web page. Or two, you can include javascript tags and begin typing javascript right into the web page.
To accomplish the first of the two options I suggested, you can code this line into your page:
<script type=”text/javascript” src=”whatever.js”></script>
This line assumes that you have a separate file from your webpage stored in the same directory called ‘whatever.js’. Within this file, you can simply write javascript syntax–no need for opening or closing tags–just straight javascript. Although you can nest this line into your code at any point (within reason–so your html can still be parsed) into your page, the best place to write this would be in the <head…></head> tags of your page so that all your functions and scripts will be loaded before a javascript dependent object on your web page is activated by an event handler.
To accomplish the second of the two options I suggested, you can code this line into your page:
<script type=”text/javascript”></script>
Once you do this, you can code your javascript scripts and functions right into your page. In this case, your code would be placed between the <script…></script> tags, like so:
<script type=”text/javascript”>alert(’hi’);</script>
This simple code would generate a javascript alert, with an ‘OK’ button, that says ‘hi’. And, yes, you can code multiple ‘<script…></script>’ tags into your page.
There are two immediately obvious down sides to taking the second of the two options that I gave you. One down side to coding your javascript right into your web page is that this can easily cause HTML errors. Remember, the more HTML errors that you have on your page, the harder it is for search engine spiders to crawl your page, and, in turn, the harder it is for your web page(s) and/or site to get indexed. The second immediately obvious down side to coding your javascript right into your web page is that if you get used to using multiple script tags on your page, your javascript will not be in one all inclusive easy to manage place.
My suggestion is that you take the first of the two options I suggested and write all of your javascript in one included easy to manage file.
Thanks for the tutorial. I am looking forward to some more.
Eric
September 30th, 2008
[...] html, php, actionscript, javascript, ajax, dom, css, mysql, and xml tutorials « Javascript Intro… [...]
Will Script For Food » Blog Archive » Javascript Alert
October 2nd, 2008