Home

Using a Script on a Web Page

 

Introduction to Scripts

 

Introduction

To create an interactive web page, you write code that receives and probably processes the values that a visitor creates and submits. The primary way you do this is to create a script. A script is code written in a web language.

As it happens, there are various script languages available. You will choose the one you like, based on reasons that depend on you. 

Specifying a Scripting Language

When the browser encounters a script on a web page, it needs to know in what language the script is written. This means that you must indicate this to the browser. To indicate that you are going to use a script, in the head section of a web page, create a script tag. This would be done as follows:

<%@ Page Language="VB" %>

<html>
<head>
<script>

</script>
<title>Exercise</title>

</head>
<body>

</body>
</html>

To help you specify the language of your script, the <script> tag is equipped with an attribute named LANGUAGE. Therefore, assign the name of the language to this attribute. There are many scripting languages available, including VB, VBScript, JavaScript, and JScript, etc, just to name a few. For example, if you will use VBScript, assign vbscript as a string to the LANGUAGE attribute. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>
<script language="vbscript">

</script>
<title>Exercise</title>

</head>
<body>

</body>
</html>

To use Visual Basic, assign "vb" to the LANGUAGE attribute. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>
<script language="vb">

</script>
<title>Exercise</title>

</head>
<body>

</body>
</html>

To specify the way the script will be processed, the <script> tag is equipped with an attribute named TYPE. The value of the TYPE attribute is text, followed by a forward slash, and followed by the name of the language. The type is assigned to the TYPE attribute as a string. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>
<script language="vbscript" type="text/vbscript">

</script>
<title>Exercise</title>

</head>
<body>

</body>
</html>

You can do the same equivalent for Visual Basic as follows:

<%@ Page Language="VB" %>

<html>
<head>
<script language="vb" type="text/vb">

</script>
<title>Exercise</title>

</head>
<body>

</body>
</html>
 
 
 
 

For an ASP.NET web page, you must indicate that the script will be processed by the server. To support this, the <script> tag is equipped with the RUNAT attribute, to which you should assign server as a string. This would be done as follows:

<%@ Page Language="VB" %>

<html>
<head>
<script language="vbscript" type="text/vbscript" runat="server">

</script>
<title>Exercise</title>

</head>
<body>

</body>
</html>

Here is the same thing done for Visual Basic:

<%@ Page Language="VB" %>

<html>
<head>
<script language="vb" type="text/vb" runat="server">

</script>
<title>Exercise</title>

</head>
<body>

</body>
</html>

After creating a script tag, you can include the desired code in it. For example, you can declare a variable. You can then use that variable locally in the script section or use it in the <% ... %> section of the web page. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language= "vb" type="text/vb" runat="server">
    Dim FullName As String
</script>

<title>Exercise</title>

</head>
<body>

<%
    FullName = "Paul Bertrand Yamaguchi"
%>

<%
    Response.Write("<br>Full Name: " & FullName)
%>

</body>
</html>

Remember that many other scripting languages are available. Because of their flexibility, for the rest of our lessons, we will mostly use VB and VBScript script languages.

The Scope and Lifetime of a Variable 

 

Local Variables

The scope of a variable determines the areas of code where the variable is available.

Global Variables

A variable that is declared outside of a delimiting area is referred to as global. For example, you can declare a variable in the script section in the head section. To declare a global variable, use the same formula as we have done so far.

 
 
   
 

Previous Copyright © 2009-2013 FunctionX, Inc. Home