JavaScript and VBScript


Scripting is another type of programming, but it's easier to learn, which is a plus. Scripts can be added to an HTML document using the <script> tag. The tag can appear within the <head> or <body> of the document.

A script might be contained in a separate document that is called by the <script> tag (much as a linked style sheet is a separate document called by the <style> tag). A script might also be contained within the <script> tags in the HTML document itself. The decision is yours, based on how often you plan to use the script. If the script appears in only one page, incorporate the script into the document, as in the first of the following HTML samples. If the script appears on more than one page, make it a separate file so that you don't have to duplicate it, as in the second HTML sample.

<script type="text/vbscript">enter your script here.</script> <script type="text/javascript"          src="/books/1/174/1/html/2/http://www.myweb.com/scripts/myscript.jss"> </script>


Although the src (source) attribute is only required when the script is contained in a separate file, the type attribute is always required. This attribute tells the browser which language the script is written in: text/javascript, text/vbscript, or text/tcl. If you are using the same scripting language throughout your HTML document, you can include a <meta> tag that defines the default script type for the entire document. The <meta> tag (as you learned in Lesson 3, "Adding Text and More") is placed inside the <head> tag and gives the browser information about the document.

<meta http-equiv="Content-Script-Type" content="type">


What Can Scripting Do?

The easy answer to this question is anything. If you look at some of the script collections on the Web, you find that people are using script for all kinds of thingsincluding adding table values, creating rollover effects, and even games.

Tip

WebDeveloper has more than 7,000 download able JavaScript samples at http://webdeveloper.com/javascript/.


It is possible to associate a script with a certain event that occurs when the page appears on the browser. Figure 14.2 is an HTML page with some very simple JavaScript code that changes the background color of the page with the press of a button.

Figure 14.2. Simple JavaScript code to change the background color with the onclick command.


You might wonder where the code is because the <script> tag is empty. The code, in this case, is embedded in the <input> tag with the onclick command. The tag responds to each of the events shown in Table 14.2 and a few more.

Table 14.2. Script Calls

Event

With Tags

The Script Runs When...

onload

<body>, <frameset>

The document opens.

onunload

<body>, <frameset>

The document closes.

onclick

Anything

The mouse is clicked over a particular item (button, image, and so on).

ondblclick

Anything

The mouse is double-clicked over a particular item.

onmouseover

Anything

The mouse is moved onto an item.

onmouseout

Anything

The mouse moves away from an item.

onmousemove

Anything

The mouse is moved while on an item.

onsubmit

Submit button

The form is submitted.

onreset

Reset button

The form is reset.


Tip

The event handlers (script calls) that tags support differ between browsers. Netscape supports many fewer event handlers than Internet Explorer does. Use Web Developer Journal's compatibility table (http://webdevelopersjournal.com/articles/javascript_limitations.html) to choose the right handler for your pages.


The scope of this book does not allow for coverage of all these topics in any depth, but I hope you have some idea of the possibilities and can take the time to learn more on your own. Table 14.3 lists the HTML tags that were discussed in this lesson.

Table 14.3. HTML Tags Used in This Lesson

HTML Tag

Closing

Description of Use

<object>

</object>

Embeds an object, such as an application, into a Web page.

<script>

</script>

Embeds a script into a Web page.


In this lesson, you've learned:

  • How to add Java and ActiveX applications to your HTML documents with the <object> tag.

  • How to add JavaScript and VBScript to your HTML documents with the <script> tag.

  • That DHTML combines all HTML, style sheets, and scripting to create Web pages that are interactive and easy to update.



Sams Teach Yourself HTML in 10 Minutes
Sams Teach Yourself HTML in 10 Minutes (4th Edition)
ISBN: 067232878X
EAN: 2147483647
Year: 2004
Pages: 151
Authors: Deidre Hayes

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net