Teach yourself vbscript in 21 days pdf free download
Friendly, accessible, and conversational, this book offers a practical grounding in the language, without ever becoming overwhelming or intimidating. Week 1 introduces the basic building blocks of the Java programming language: keywords, operators, class and object definitions, packages, interfaces, exceptions, and threads.
Week 2 covers the Swing graphical user interface class libraries and the important classes that support data structures, string handling, dates and times.
Week 3 ventures into the hottest areas of Java programming: web services, Java servlets, network programming, database programming and Android development. Mastering HTML, CSS, and JavaScript is vital for any beginning web developer - and the importance of these technologies is growing as web development moves away from proprietary alternatives such as Flash.
With this book, beginners can get all the modern web development knowledge you need from one expert source. Meloni covers all the building blocks of practical web design and development, integrating new techniques and features into every chapter.
Covers everything beginners need to know about the HTML and CSS standards and today's JavaScript and Ajax libraries - all in one book, for the first time Integrated, well-organized coverage expertly shows how to use all these key technologies together Short, simple lessons teach hands-on skills readers can apply immediately By best-selling author Julie Meloni Mastering HTML, CSS, and JavaScript is vital for any beginning web developer - and the importance of these technologies is growing as web development moves away from proprietary alternatives such as Flash.
This third edition improves on these qualities while updating the material to cover the latest developments in JavaScript. Written by an expert technical writer, it has been acclaimed for its clear and personable writing, for its extensive use of examples, and for its logical and complete organization.
Tip If you want to disable a submit or reset button, then you can do so using a function that returns the boolean value false. Form Elements Forms can have, in principle, any number of child elements of several different types. Selection-based elements include check boxes, radio buttons, and select boxes. Control elements include buttons, for example.
Note that only certain properties of form elements can be changed. Some are read-only. As a general rule, if the page needs to be restructured in order to implement a change made by a script, then that change is not allowed.
For example, the type attribute of the element is read-only. Similarly the defaultValue property is also read-only. If any of your other code makes use of the original value of the name property before you made a change, then the output of some parts of your code could be unpredictable. Textual elements do, however, allow their content, and hence appearance, to be changed. This includes not only changes made by a user but also changes made by a script.
Therefore we can change the value of a text area to change its content, or even change the value of a button to change the text it shows. Caution Netscape Navigator 4 does not resize buttons if you alter the value of the value property of a button object with the intention of relabeling the button.
Any text that is longer than the original length that you assign to the value property will simply be truncated if it is too large for the existing button dimensions. The elements Collection As well as accessing each element of a form by name, it is possible to access all of them through a collection of the document object.
Each form that belongs to the forms collection contains another collection called the elements collection. This collection brings all the elements in the form together so as to make them accessible as an array. If for example a table has been used inside the form to lay out certain elements, the elements collection will skip through the table straight to the elements you need to access.
Therefore, you could in theory access the third element of the second form on a page by using this notation, as shown here: document. It can be fragile. Suppose you insert an additional element early in a form, then your code for all later form elements will no longer work correctly because they now are in a different position in the elements array.
To access the first form on the page use the following: document. So far we have accessed forms through the document object and referred to the form by name. But when a script or function is called from the form or an element in the form itself we can use a shortcut. This shortcut is made available through a special JavaScript keyword that refers to the object that the script was called from: the keyword this.
This can save you from navigating through the hierarchy of objects as well as shorten the amount of code you have to write. Whether you decide to pass a reference to the present object using this , its form this. If you can avoid referring to the window object, then you can also avoid using the keyword this. When used in other locations, the object it refers to is different and so it can be very useful. Listing 6. When space is limited this technique can save you from using extra text outside the element explaining what it is for and taking up space.
Clearly anyone who wants to send his e-mail address to subscribe has to delete this text from the text box first—unless we do it for the user. And that is exactly what the onFocus event handler that has been placed in the tag does.
The this keyword bypasses all this however. By using it you are saved from lengthier references that include the document and form name in the object path, as well as sometimes being saved from naming the form concerned.
When calling functions using event handlers located in the opening tags of an element this can be very useful. The this keyword can be used to send data more than once. You have just seen how it can be used. Form elements also typically have a name property, which corresponds to the name attribute on the start tag in the HTML.
When we come to validate data entered into a form, we must be able to access the value entered. We will look in a moment at how that is done, as well as some other issues relevant to individual types of the form element. The property belonging to text-based form elements that we will make most use of is the value property.
File upload elements, as we will refer to them from now on, cannot be set using scripts. If we could then we would be able to upload files from the computer of anyone visiting our site simply by filling in these elements with a likely path to a file.
Obviously this could enable serious security breaches and invasions of privacy, hence the restriction. To access a form element by name, the element must, of course, have a name attribute in its start tag. Even if the element is nested inside other elements such as a table, we can still access it as if it was a direct child element of the form.
Again this is because forms have been around for a long time and shortcut routes to an object were the original means of accessing elements. Hence we can access a text box by simply writing something similar to the following: document.
The text box is still a full object, and the text it contains is one of its properties. You may already have guessed that if you can place text in a text box by giving it a value attribute in its opening tag that this will be the property name used to access it as well. If you did then you are absolutely correct. The onclick attribute is an event handler; however, whereas the onload event handler triggers when the page loads, the onclick event handler detects when a user clicks on the button.
In the listing it calls the function alertText when users click on the button. Note the syntax of the onclick event handler, as we will use it often when using buttons to call a function.
In HTML they are often written with uppercase only. This is not an option for event handler names in JavaScript. Again, JavaScript is case sensitive, and if we assign an event handler using JavaScript we must use correct case letters. HTML Forms and the String Object Once the button on the page has been clicked and the function alertText is called, the first thing that happens is that the function retrieves the text from the text box.
It does this through the first statement. The value of the value property when found is assigned to the variable called text before the next line when the alert function is used to bring up an alert box that contains its value for us to see.
Caution One important point to note about values retrieved from forms is that they have the data type string—even if the characters in the form element concerned were purely numeric. We will come back to this point as it arises. Getting access to the value entered into a text element is straightforward since the text element is a child of the form and the text element has a value property.
You can use the focus method of the text property of the form object to attach focus to the desired field. Doing that is more convenient to the user. The dimensions of the text area are specified by the values of the rows and cols attributes.
Text areas could be used, for example, to collect customer feedback comments or for detailing delivery instructions for an online purchase. If you want to access the value of a text area, you can use syntax similar to that which was used for the text field shown earlier.
In some circumstances, you may want to disable some form elements to ensure that data has been entered elsewhere in the form before allowing the user to enter comments, for example. For each element that is a checkbox we display an alert which states the position of the checkbox in the elements array, and whether it is checked or not.
If the member of the elements array is not a check box then we do nothing, as indicated by the else clause towards the end of the CheckCheckboxes function. Tip If you want to ensure that a user has consciously checked one of a set of radio buttons, then leave them all unchecked when the page loads and then verify, before the form is submitted to the server, that a radio button has been checked.
If no radio button has been checked, then provide a reminder for the user that one of the radio buttons provided must be checked. HTML Forms and the String Object Radio buttons are, as you perhaps know, grouped together by giving individual radio buttons the same name.
Initially this may seem to create a problem if you want access to these elements using JavaScript. The solution is that JavaScript treats radio button groups as an array. The above line of code would effectively address the array, and to access any of the individual elements you would need to specify an element index.
The element that comes first in the HTML that makes up the page is the first element in the array, the element that comes second on the page is second in the array and so on. Caution Remember that the first element in an array is numbered as zero. The array representing a radio button group has all the properties that you would expect to find in the arrays that you create yourself. For example there is a length property for the array that tells you how many entries there are in the array, or in radio button terms, how many radio buttons there are in a radio button group.
Each radio button in a group is an object in its own right with its own individual properties. There is no one property that you can examine to discover which radio button in a group of radio buttons is the radio button that has been selected by the user if any.
You will need to examine each button in turn to determine whether it is selected or not. Here the length property comes in useful once again. In Listing 6. It will simply store the characters one after another and leave the choice of processing up to us—and for that of course we will need tools. Fortunately, the String object provides many methods to process the content of strings.
When the JavaScript interpreter comes across a string literal that contains some characters or none at all in the case of the empty string it gives these characters the data type string and stores them.
String Properties The String object has only one property that you are likely to make use of on a regular basis. This property is the length property. There are another two properties, which are the constructor and prototype properties that we have mentioned also exist for the Array object.
Just as the length property of the Array object tells you how many elements there are in an array, the length property of the String object tells you how many characters exist in a string.
However, the length property is very useful when it comes to string manipulation. Just as the length property of the Array object can be used to help our scripts reliably determine where to find the end of an array, the length property is used to determine where a string ends.
For string manipulation this information is often very useful. String Methods The String object provides a very large number of methods.
Some of these methods will help you when working with your strings—they are listed below. Examples of how to use some of these methods will follow. You probably expect that it is possible to apply the methods to data containers that contain a string value. In fact, your applications can be more complex than this. As usual when multiple operators that are the same or have the same operator precedence are used in an expression, the operands of the member operator.
Once the JavaScript interpreter has parsed this, it then applies the next operand to the right which in this case is the valueOf method. This method, as we know in the case of literals, simply returns the string itself.
Once this result from applying the valueOf method has been calculated, then the next and final expression is applied to the result using the member operator. As its name implies, this string method converts to uppercase all the characters of the string to which it is applied. Simple Case Transformations with toUpperCase When you need to compare strings you can run into the problem that lowercase and uppercase letters have different character codes.
If the comparison you want to make is independent of case, then you will need to convert all of the string, or more precisely a variable which holds that value, to a case which is the same as the case of the variable with which it is being compared. The output of the document. It is the position of the first character of the substring being searched for that is found using the indexOf method. This is similar to a search and replace function in a word processor.
For example, if you had a description of JavaScript but wanted to be sure that all mentions had the correct case, then you could use the replace 6 Day 6 method to replace any incorrectly capitalized versions in the text. If IsFormValid returns false the form is not submitted.
If any of the three tests are false, then the DisplayErrors function is called. Three if statements within the DisplayErrors function control which alert boxes display. Then, and only then, can the form be submitted to a server. The tests we have applied to the text field, the radio buttons, and the text area are very simple ones—we have only checked to see that some data has been entered and that none of the three essential fields has been left blank.
In other circumstances, you may want to check for the length of number when checking credit cards, for example , whether a value entered is a valid date, and so forth. In production forms you may want to carry out more sophisticated checks on data. If you expect, for example, that a valid date has to be entered for a particular piece of data, then you would check if it was a valid JavaScript date.
Summary In this chapter we have introduced you to the use of JavaScript with the Form object and form elements. You have been shown how to check values entered by the user, to assist the user by applying focus to a field you want to be filled in first, and how to prevent data from being sent to the server for processing while it is incomplete. In addition you have been introduced to some of the properties and methods of the object.
String 6 Day 6 Workshop In this workshop we will review what you have learned about forms in this chapter. Do forms have to be embedded in tables to be displayed on screen? However, using tables makes it possible to line up parts of the form to achieve an attractive presentation. An alternative approach is to use CSS for positioning. Yes, you can. However you will need to think carefully about how data is to be submitted to the server for processing.
For instance, you may want to include a submit button only on the second form. Is it possible to use patterns of characters to search for sub-strings? Yes, JavaScript has a RegExp object that allows you to use regular expressions. Regular expressions make it possible to search strings for patterns of characters in addition to searching them for literal sub-strings. Are password fields on forms totally secure? A password field keeps someone from looking over your shoulder seeing on screen the password characters you have typed, but when your password is sent to the server it is not encrypted or disguised in any way.
Quiz 1. List the form elements into which you can type text. How do you create a drop-down menu in a form?
Quiz Answers 1. There are three form elements into which you can type text—the text field, the text area, and a password field. Of course, with the password field you see only an asterisk for each character you type in.
Create a form that asks for a name and credit card information. Write a script that checks that each field has been filled in before the data is sent to the server. The Core JavaScript Number and Math objects provide some powerful mathematical tools to make many of the processes and calculations much easier.
As you will see, even when our specific requirements are not covered directly by what JavaScript provides, we can frequently create our own tools with a little bit of ingenuity. The number of possible JavaScript uses for numbers is enormous.
Unless you are particularly interested in math, then you will find that JavaScript provides many mathematical techniques that you may never use. However, in common with all worthwhile programming languages, JavaScript needs to provide powerful and flexible facilities that will allow interested programmers to manipulate numbers.
This chapter will introduce you to some foundational numeric techniques that you can build on if you are interested in this topic. Usually you only use its methods, which can be very helpful—especially those methods that have been recently introduced in JavaScript 1.
By default both Internet Explorer and Netscape Navigator will return floating-point numbers rounded to 16 or 17 significant figures. For example, if we divide On the element there is an onload attribute which calls the Calculate function.
Within the Calculate function we use an alert box to inform the user what the page will do. We use three prompt boxes to gather three numbers chosen by the user. Caution When you enter a numerical value in a prompt box the value is held as a string. If you attempt to perform calculations then you can expect an error since the value on which a calculation is being attempted is a string.
To avoid such errors you need to convert the numeric-looking string to a number using the Number function. We used document. Within the document. Once we do reach the decimal point, then we loop again until we have moved the required number of positions through the string variable; then we simply return the num string variable to the document. The result of dividing Controlling the format when you expect integer numbers is easier, if you are sure that the result will be an integer.
Listing 7. Then, in a prompt box, the user is asked to enter either a number or a string. The Number function is then applied to the string—held in the entry variable— that the user had entered.
Finally the isNaN function is used to test whether or not a valid number resulted from applying the Number function to the entry variable. If no valid number resulted, then the user is shown the string they entered and informed that it was not a number. If, however, the entry variable can be converted into a valid number, then the user is reminded of her entry into the prompt box and told that it was a number.
Figure 7. So, if you want to test whether a value is or is not a number remember to use the isNaN function. The other properties of the Number object reveal key JavaScript values. Note that the name of each property is written completely in uppercase letters. On page load the CreateExponentials function is called. A prompt box is used to get a floating-point number from the user. Because the toExponential method belongs to the Number object, you convert the string value entry1 to a numeric value stored in the number variable.
ANALYSIS The listing simply loops through a for loop displaying the value entered by the user with the number of decimal places indicated by the loop counter variable, i, of the for loop. The toPrecision Method Although you may not have much need to display your numbers in exponential notation you will more than likely want to control their accuracy at some point or another.
To do this you have two options. You can either decide on a number of significant figures, or you can decide on a number of decimal points.
To choose the accuracy in terms of the number of significant figures, you use the toPrecision method. The method is Numbers and Math applied with the dot notation and the number of significant figures is controlled by specifying the number as a parameter of the method.
Again, remember that the toLocaleString method belongs to the Number object and that a prompt box accepts a string value. On the Regional Settings tab choose, for example, German Standard.
Caution At the time of writing although the toLocaleString method was recognized by Netscape 6, it did not seem to have any effect on the numbers to which it was applied. Remember from the previous chapter that we stated that the toLocaleString method is also applicable to arrays. If you have gathered monetary data and stored it in a string, you do not need to apply the toLocaleString method to each element of the array in turn.
You simply apply it to the array as a whole. Caution It is important to remember that the toExponential , toFixed , toLocaleString , and toPrecision methods all return their results as strings. The valueOf method The valueOf method of a Number object returns the value of a Number object as a number datatype.
If JavaScript had been created from the start as a fully object-oriented technology you might well have expected a Number object to possess a value property. When using these properties remember that they must be referred to as properties of the Math object.
If you only use the Math object occasionally, then both of these important details are easy to forget. PI property in calculating the area and circumference of a circle. The page opens with the focus set on the first element in the form, by means of an onload attribute on the element which calls the SetFocus function. The user enters three values, one in each of the elements. Each of the values entered is assigned to three variables—num1, num2, and num3. If even one of the three variables is not a number, which we test by means of the isNaN function, then an error message is output, the form is reset, and the focus is again placed on the first element in the form.
If three valid numbers are entered then a string, held in the alertString variable, is displayed using an alert box. The three numbers entered are listed in the order in which they were entered and the maximum and minimum values of the three numbers are displayed. Once the user has acknowledged the alert box then the user can enter three more numbers. Note The text you want to add to the element must be placed between the start tag and a end tag.
If you try to use a tag with attributes, then you will have only a tiny button with no text displayed on screen. The random method generates a random number between 0 and 1. So, if we wanted to create random numbers greater than 1, we would generate a random number between 0 and 1 and multiply by an appropriate number to achieve the correct range of random numbers.
The method returns a numeric value between 1 and 12 that represents the month of the year, as illustrated in Figure 9. Caution 9 Be mindful of the fact that JavaScript counts the months in the year starting with 0. Therefore, January is actually 0 and December is Make sure you remember this when you are converting dates. The Date object includes a specific method for each element of the date, as shown in Table 9. TABLE 9. Internet Explorer always returns a four-digit year.
Netscape 4 and later subtracts from the date and returns the result. Earlier versions of Netscape truncate and return a two-digit year. In fact, it is probably going to end up causing you more grief than anything.
As mentioned in the table, when Netscape Navigator versions 4 or later see the getYear method, the browser returns a year value by subtracting from the current year value. As you can imagine, this concept worked pretty well before the new millennium. For example, if the year is and you subtract you get a two-digit year of But this type of date conversion no longer Day 9 works. When you subtract from you get a value of This being said, I would recommend just using the getFullYear to return the year portion of any date.
Another thing to keep in mind is the numeric range used to represent the months of the year and days of the week. Most of us are used to the concept of 12 months in a year and seven days in a week. That being said, the first month of the year is January and the last month is December. JavaScript uses a different numbering pattern in that both of these ranges start with the number zero, making the first value actually 0 instead of 1. If you want to return the actual name of the month or day you need to create code to convert the numeric value, as discussed in the following section.
Converting the Numeric Day and Month Values More than likely when capturing the day of the week or even the month of the year you are going to want to display the text equivalent of the numeric value returned.
As you noticed in Figure 9. Unfortunately if you decide to create your own date layout there are not any methods for the Date object to display the text equivalent of either the day of the week or the month value. Therefore, in order to display these values you must create your own JavaScript code to convert the numeric value returned by the getMonth method and the getDay method.
Of course, as with anything you code, there are multiple ways that you can code the conversion from a numeric value to a text value. Since the weekdays array contains 7 values from 0 to 6 the index values for the array elements match the numeric values returned by the getDate method.
Since the element of the array with an index value of 2 is Tuesday, the JavaScript code displays that value on the page, as illustrated in Figure 9. Of course you can create the same type of code to convert the numeric value returned by the getMonth method into the name of the month. Again, keep in mind the fact that the getMonth method returns a numeric value between 0 and 11 representing the month. Therefore, you need to make sure you design your code so that 0 represents January and 11 represents December, so that you will get the correct conversion of the numeric value returned by the method.
Obviously the main reason for manipulating the contents of the Date object would be to format the date value to match your own requirements for the date output. In order to display the date in this fashion you cannot just dump the contents of the Date object, as shown with Figure 9. However, making the date appear as you want is still quite simple. As you are aware, when you concatenate values together you are essentially creating one string of values from the original string.
If you want to display the current date in the format mentioned above with the month followed by the day and year values, you need to concatenate those values together as illustrated in Listing 9. When concatenating values together, remember you need to manually insert any spacing that you want in your string. Figure 9. As you can probably imagine, there are an infinite number of ways you can express a date, especially once you have retrieved the elements of the date that you need to display.
You can combine any of the time information with your date statement for even more ways to customize your date statement. Time Formatting It seems quite odd to be looking in the Date object for time information, but that is where you will find it when working in JavaScript. As you remember from our discussion earlier in this chapter, JavaScript stores the information in the Date object as a millisecond value.
In other words, the value of the Date object is a long number indicating the total number of milliseconds that passed between January 1, and the time when the Date object was created. So you see, although the object is called Date, it actually indicates a time value by specifying the amount of time since that date.
For example, you can use the getHours to return the number of hours in the time portion of the Date object, as shown in Listing 9. You will probably quickly notice that there are no AM or PM times. The value returned by the getHours method is in a hour clock format or what is commonly referred to as military time, as shown in Figure 9.
Obviously there are other methods that you can use to return the other portions of the time. Table 9. The method returns a value between 0 and 23, where 0 represents midnight and 23 represents PM.
The value returned is in the range of 0 to The getTime method basically just returns the numeric value that JavaScript stores for the Date object.
This value is the same as the value that JavaScript uses to store the date. The time values returned from the Date methods can be combined with the same method used to create a custom date. For example, you can display an hour and minute combination by combining the values returned by the getHours method with the getMinutes method value. Of course all of these times will be based upon a hour clock, so if it is PM, the time displays as The only way to remedy this is to manually convert the time value before displaying it.
Even if you like stating it as hours, visitors to your site most likely would prefer seeing the time displayed in the 9 Day 9 more traditional fashion of PM.
That being said, JavaScript obviously does not provide a default solution for doing this. If you want to display the AM and PM times, you need to manually code a method to convert the value returned by the getHours method to the desired format. If you are familiar with the hour clock concept, you know that the hours start counting at midnight with 0.
Noon is considered to be which is followed by commonly referred to as PM. With a hour clock, each hour gets progressively higher until , or PM.
Of course, at midnight the counting starts over. Listing 9. For example, if the time is the code converts it to PM, as illustrated in Figure 9. But there is one small issue. Since the values that are returned are all integer values it only displays single digits for values less that This can make a time like look a little odd when it displays as To fix this problem, you can add additional if statements that look at the value returned by the getMinutes and getSeconds methods, and adds a zero to the front if the value is less than 10, as shown in Listing 9.
If either of the variables contains a value less than 10, a value of zero is placed in front of that value. Of course, the time zone setting is only accurate if the user has properly set it. In order to do this you can use the getTimezoneOffset method of the Date object to determine the number of minutes between the current time zone and GMT.
Thus if you run Listing We can use longer literal strings using the same syntax. Listing If you choose other single characters, you will receive a message indicating that you made an incorrect guess. ANALYSIS If you tried to enter a multi-character string beginning with the correct character, then you will get a message erroneously indicating that you made a correct choice.
Why is that? The reason behind the apparently incorrect behavior is that length of a string is an independent characteristic from the pattern. In other words, a pattern seeks a match for itself but also accepts longer matches which begin with the stated pattern. So, if you want to match a pattern and a length at the same time, you would need to test for the string length as well as whether it matches the regular expression.
Suppose you wanted to ensure that the identifier for a machinery part started with an alphabetic character. How could you do that? You could use the pipe character and add a total of 52 choices assuming that all alphabetic characters were valid. The square bracket syntax allows a much more compact way of expressing those multiple choices. In Listing In which case, we would define a new character class that contains only the uppercase alphabetic characters from A to M inclusive.
Suppose we have a parts catalog that has a structure of two uppercase alphabetic characters, a dash, and three numeric characters. In that case, we have three digits followed by a dash followed by two digits followed by a dash followed by four digits. Patterns Using Variable Numbers of Occurrences We have made much progress in solving the problems discussed earlier. By adding syntax to allow for variable numbers of characters, we can add yet more power and flexibility to our growing repertoire of regular expressions.
We need a syntax that allows for zero or one space character between the groups of four numeric digits. Since we are now allowing the user to possibly omit spaces, we cannot continue to use a simple fixed length for the credit card number string. The shortest possible length is 16 characters when all spaces are omitted , and the maximum length is 19 characters when all single space characters are present.
It will still report an error when a user enters an alphabetic character, or if a user inserts a space anywhere but between the allowed four numeric characters from the defined character class. ANALYSIS It will, for example, limit domain names to alphabetic characters, numeric characters, and a dash hyphen in accordance with the domain-naming conventions. However, it has limitations in a number of respects.
As currently written, it will incorrecetly indicate an error for the valid but fictional e-mail address of [email protected] 13 Day 13 It will also accept as its final three characters, any three alphabetic characters.
This edition strengthens its focus on C programming fundamentals, and adds new material on popular C-based object-oriented programming languages such as Objective-C. Filled with carefully explained code, clear syntax examples, and well-crafted exercises, this is the broadest and deepest introductory C tutorial available. Friendly and accessible, it delivers step-by-step, hands-on experience that starts with simple tasks and gradually builds to professional-quality techniques.
Each lesson is designed to be completed in hour or less, introducing and clearly explaining essential concepts, providing practical examples, and encouraging you to build simple programs on your own. Sams Teach Yourself C in 21 Days, Fifth Edition, presents C in the most logical and easy-to-learn sequence, and is geared towards programmers learning the C language.
This book discusses some C features that allow rapid development of solutions such as garbage collection, simplified type declarations, and scalability support.