300 likes | 451 Views
6. Functions and Procedures. Internet Programming Using VBScript and JavaScript. 6. Objectives. In this chapter you will: Use built-in functions in JavaScript and VBScript Create functions in JavaScript and VBScript Call functions in JavaScript and VBScript
E N D
6 Functions and Procedures Internet Programming Using VBScript and JavaScript
6 Objectives • In this chapter you will: • Use built-in functions in JavaScript and VBScript • Create functions in JavaScript and VBScript • Call functions in JavaScript and VBScript • Create and call procedures in VBScript • Create event procedures in VBScript
6 Built-In Functions and Methods • Both JavaScript and VBScript support several built-in objects and built-in functions • When an object contains a built-in function it is referred to as a method • Built-in functions include mathematical, date and time, string, and formatting functions, among others
6 JavaScript Math Object Methods • A method is a function that is defined within an object • To access these methods on some platforms, you will need to specify the fully qualified name of the object • These mathematical methods are useful when you work with numbers
6 JavaScript String Object Methods • You can explicitly define a string object with the keywords new String, as in varName = new String(“George”) • The string object exposes several built-in properties and methods • Table 6-2 identifies a few of the common string methods • Some of these string methods could be replaced with basic HTML tags
6 Common Methods of the String Object
6 Common Methods of the String Object
6 The toString Method • The toString method belongs to several objects, including the string number, and math objects • The toString method of the number object converts a number to a string • The toString method of the math object is used to convert a math object to a string • The toString method of the string object will return the string • The toString function can also be used to convert a function to a string
6 JavaScript Global Functions • When a value is passed from a form, the value is passed as a string • You cannot perform mathematical calculations on strings • You will need to convert string variables and strings to numbers • In JavaScript, there is a global object that contains functions that are not associated with other JavaScript objects
6 The Eval Function • The eval function of the global object will evaluate a string of JavaScript statements that can consist of expressions, variables, functions, and objects • The eval function can do more than just convert a string to a number • It evaluates the statement and returns the result • The eval function is useful when you receive a number as a string object from a form, and need to convert it back to a number
6 The isNaN Function • If you attempt to use a nonnumeric value in a mathematical equation, the script will cause an error • In Javascript, the isNaN function, known as the “is not a number” function, can determine whether the value is something other than a number • The isNaN function returns false if the value is a number, or true if the value is not a number
6 The ParseInt and ParseFloat Functions • The parseFloat function converts a string into a floating-point number • The parseInt function converts a string into a number • The parseInt function takes two parameters, the string and the radix • The radix identifies which base numbering system you want to use when converting the string to a number
6 VBScript Built-In Functions • VBScript supports a wide range of built-in functions that can be called from any script • Although some functions are unique to each scripting language, some functions have different names but perform similar tasks • In VBScript the isNumeric function is used to determine if the value of a variable is numeric or not • VBScript supports additional built-in data conversion such as cInt, which converts a string to an integer
6 Common VBScript Built-In Functions
6 VBScript Built-In Functions • VBScript includes two built-in functions that display information within a browser • The InputBox function is similar to a prompt window • The MsgBox function is similar to the alert window • The MsgBox function displays a message and one or more buttons
6 VBScript Built-In Functions • It is important to recall that Netscape Navigator does not support VBScript • To create compatible scripts, you will need to use JavaScript built-in functions • However, if you are writing server-side scripts, you can use any of these built-in functions, since the functions are executed on the server, and not on the client’s browser
6 Using Functions • A function is a block of code that is grouped into a named unit • The function is identified by a unique name • JavaScript and VBScript both use the keyword function to identify the start of a function • A pair of parentheses follows the name of the function • VBScript can be public or private • Public functions are visible to all other functions in the script
6 Using Functions • Private functions are only available within the script in which they were declared • A function can return a value to the script that called it, but it is not required to return a value • If a function does return a value, it can only return one value • In VBScript a script can use the keyword call to call a function • The call keyword is optional, but if it is used, then the arguments, passed to the function must be enclosed in parentheses
6 Calling a Function from an Event Handler • Functions are often called when an event occurs • Event handlers can be used to intercept an event and to cause the program to call a user-defined function • To create a Web page that uses a function to process a form, perform the steps on pages 223 and 224 of the textbook
6 Passing an Argument to a Function • You can see how events, methods, and functions can be combined to create an interactive Web page • The user can change the color of the background of the Web page • When the user clicks a button, the onClick event handler will call a function, and pass an argument to the function • Each event handler will call the same function, but will pass a different argument
6 Passing an Argument to a Function • To create a Web page that uses an argument to pass a value to a function, use the instructions on pages 225 to 227 of the textbook
6 Passing an Argument to a Function • To create a Web page that uses a function to change the status message, follow the directions outlined on pages 227 and 228 of the textbook
6 Returning a Value from a Function • There are some differences between VBScript and JavaScript functions • JavaScript functions return a value using the keyword return, and the value • VBScript functions assign the value to the function • To create a Web page that uses a function to return a value, follow the procedures on pages 229 and 230 of the textbook
6 Using Procedures • A procedure is similar to a function, in that it is a block of code that can be called from other locations • This allows you to create blocks of code that can be reused within your Web application • Instead of writing the same code over and over again, you only need the procedure call to reference the code • Like a function, a procedure can accept zero or more arguments
6 Using Procedures • Variables declared within the procedure are local and only available to the procedure • The main differences between functions and procedures are that procedures do not return values, and they cannot be used in an expression • Programmers refer to procedures as sub-procedures because they are declared using the keyword sub • You can use the statement exit sub to exit the procedure
6 Using Procedures • The end of the procedure is identified with the keywords end sub • The sub statement is referred to as a procedure call • Procedure calls look and act like regular VBScript statements (an example of the syntax is located on page 231 of the text. • Procedure calls can be used more than once and anywhere within your application, including within other procedures, functions, or control structures
6 Using Procedures • The syntax for calling a procedure in VBScript is as follows: [call] procedureName(argument) • Create a Web page that uses procedures to calculate the total cost of a product and display the results to the user with the procedures on pages 232 and 233 of the textbook
6 Event Procedures • Events such as click and load would be intercepted by event handlers such as onClick and onLoad • This method is supported in both JavaScript and VBScript • VBScript also allows you to intercept the event using an event procedure • An event procedure is a type of procedure that is attached to an object
6 Event Procedures • Event procedures are named according to the name of the object and the event name • An underscore (_) is used to separate the object name and the event name • Refer to the example syntax used for creating event procedures on page 234 of the textbook • To create a Web page that uses an event procedure to call another procedure, perform the steps on page 234
6 Summary • Both VBScript and JavaScript support built-in functions • You can create functions, which are blocks of code that can be reused • The blocks of code are assigned a name • A function can be used in an expression because it can return a value to the script that called it, using the keyword return