This document discusses JavaScript objects and methods for manipulating strings and performing mathematical calculations. It introduces the Math object which allows common mathematical operations and contains constants like PI. It also covers the String object which allows manipulating and processing strings, including character-level methods, searching/extracting substrings, and generating XHTML tags. Methods like split(), indexOf(), toLowerCase() are described.
SearchingStrings
.html
2 of 3
26searchForm.last12.value =
27 letters.lastIndexOf(
28 searchForm.inputVal.value, 12 );
29 }
30 // -->
31 </script>
32
33 </head>
34 <body>
35 <form name = "searchForm" action = "">
36 <h1>The string to search is:<br />
37 abcdefghijklmnopqrstuvwxyzabcdefghijklm</h1>
38 <p>Enter substring to search for
39 <input name = "inputVal" type = "text" />
40 <input name = "search" type = "button" value = "Search"
41 onclick = "buttonPressed()" /><br /></p>
42
43 <p>First occurrence located at index
44 <input name = "first" type = "text" size = "5" />
45 <br />Last occurrence located at index
46 <input name = "last" type = "text" size = "5" />
47 <br />First occurrence from index 12 located at index
48 <input name = "first12" type = "text" size = "5" />
49 <br />Last occurrence from index 12 located at index
50 <input name = "last12" type = "text" size = "5" /></p>
window.html
1 of 7
1<?xml version = "1.0" encoding = "utf-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 12.13: window.html -->
6 <!-- Using the Window Object -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Using the Window Object</title>
11
12 <script type = "text/javascript">
13 <!--
14 var childWindow; // variable to control the child window
15
16 function createChildWindow()
17 {
18 // these variables all contain either "yes" or "no"
19 // to enable or disable a feature in the child window
20 var toolBar // specify if toolbar will appear in child window
21 var menuBar; // specify if menubar will appear in child window
22 var location; // specify if address bar will appear in child window
23 var scrollBars; // specify if scrollbars will appear in child window
24 var status; // specify if status bar will appear in child window
25 var resizable; // specify if the child window will be resizable
42.
window.html
2 of 7
26
27// determine whether the Tool Bar checkbox is checked
28 if ( toolBarCheckBox.checked )
29 toolBar = "yes";
30 else
31 toolBar = "no";
32
33 // determine whether the Menu Bar checkbox is checked
34 if ( menuBarCheckBox.checked )
35 menuBar = "yes";
36 else
37 menuBar = "no";
38
39 // determine whether the Address Bar checkbox is checked
40 if ( locationCheckBox.checked )
41 location = "yes";
42 else
43 location = "no";
44
45 // determine whether the Scroll Bar checkbox is checked
46 if ( scrollBarsCheckBox.checked )
47 scrollBars = "yes";
48 else
49 scrollBars = "no";
50
43.
window.html
3 of 7
51// determine whether the Status Bar checkbox is checked
52 if ( statusCheckBox.checked )
53 status = "yes";
54 else
55 status = "no";
56
57 // determine whether the Resizable checkbox is checked
58 if ( resizableCheckBox.checked )
59 resizable = "yes";
60 else
61 resizable = "no";
62
63 // display window with selected features
64 childWindow = window.open( "", "", "resizable = " + resizable +
65 ", toolbar = " + toolBar + ", menubar = " + menuBar +
66 ", status = " + status + ", location = " + location +
67 ", scrollbars = " + scrollBars );
68
69 // disable buttons
70 closeButton.disabled = false;
71 modifyButton.disabled = false;
72 getURLButton.disabled = false;
73 setURLButton.disabled = false;
74 } // end function createChildWindow
75
44.
window.html
4 of 7
76// insert text from the textbox into the child window
77 function modifyChildWindow()
78 {
79 if ( childWindow.closed )
80 alert( "You attempted to interact with a closed window" );
81 else
82 childWindow.document.write( textForChild.value );
83 } // end function modifyChildWindow
84
85 // close the child window
86 function closeChildWindow()
87 {
88 if ( childWindow.closed )
89 alert( "You attempted to interact with a closed window" );
90 else
91 childWindow.close();
92
93 closeButton.disabled = true;
94 modifyButton.disabled = true;
95 getURLButton.disabled = true;
96 setURLButton.disabled = true;
97 } // end function closeChildWindow
98
45.
window.html
5 of 7
99// copy the URL of the child window into the parent window’s myChildURL
100 function getChildWindowURL()
101 {
102 if ( childWindow.closed )
103 alert( "You attempted to interact with a closed window" );
104 else
105 myChildURL.value = childWindow.location;
106 } // end function getChildWindowURL
107
108 // set the URL of the child window to the URL
109 // in the parent window’s myChildURL
110 function setChildWindowURL()
111 {
112 if ( childWindow.closed )
113 alert( "You attempted to interact with a closed window" );
114 else
115 childWindow.location = myChildURL.value;
116 } // end function setChildWindowURL
117 //-->
118 </script>
119
120 </head>
121
122 <body>
123 <h1>Hello, This is the main window</h1>
46.
window.html
6 of 7
124<p>Please check the features to enable for the child window<br/>
125 <input id = "toolBarCheckBox" type = "checkbox" value = ""
126 checked = "checked" />
127 <label>Tool Bar</label>
128 <input id = "menuBarCheckBox" type = "checkbox" value = ""
129 checked = "checked" />
130 <label>Menu Bar</label>
131 <input id = "locationCheckBox" type = "checkbox" value = ""
132 checked = "checked" />
133 <label>Address Bar</label><br/>
134 <input id = "scrollBarsCheckBox" type = "checkbox" value = ""
135 checked = "checked" />
136 <label>Scroll Bars</label>
137 <input id = "statusCheckBox" type = "checkbox" value = ""
138 checked = "checked" />
139 <label>Status Bar</label>
140 <input id = "resizableCheckBox" type = "checkbox" value = ""
141 checked = "checked" />
142 <label>Resizable</label><br/></p>
143
144 <p>Please enter the text that you would like to display
145 in the child window<br/>
146 <input id = "textForChild" type = "text"
147 value = "<h1> Hello, I am a child window</h1> <br>"/>
47.
window.html
7 of 7
148<input id = "createButton" type = "button"
149 value = "Create Child Window" onclick = "createChildWindow()" />
150 <input id= "modifyButton" type = "button" value = "Modify Child Window"
151 onclick = "modifyChildWindow()" disabled = "disabled"/>
152 <input id = "closeButton" type = "button" value = "Close Child Window"
153 onclick = "closeChildWindow()" disabled = "disabled"/></p>
154
155 <p>The other window's URL is: <br/>
156 <input id = "myChildURL" type = "text" value = "./"/>
157 <input id = "setURLButton" type = "button" value = "Set Child URL"
158 onclick = "setChildWindowURL()" disabled = "disabled"/>
159 <input id = "getURLButton" type = "button" value = "Get URL From Child"
160 onclick = "getChildWindowURL()" disabled = "disabled"/></p>
161
162 </body>
163 </html>
cookie.html
1 of 4
1<?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 12.15: cookie.html -->
6 <!-- Using Cookies -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Using Cookies</title>
11
12 <script type = "text/javascript">
13 <!--
14 var now = new Date(); // current date and time
15 var hour = now.getHours(); // current hour (0-23)
16 var name;
17
18 if ( hour < 12 ) // determine whether it is morning
19 document.write( "<h1>Good Morning, " );
20 else
21 {
22 hour = hour - 12; // convert from 24 hour clock to PM time
23
53.
cookie.html
2 of 4
24// determine whether it is afternoon or evening
25 if ( hour < 6 )
26 document.write( "<h1>Good Afternoon, " );
27 else
28 document.write( "<h1>Good Evening, " );
29 }
30
31 // determine whether there is a cookie
32 if ( document.cookie )
33 {
34 // convert escape characters in the cookie string to their
35 // english notation
36 var myCookie = unescape( document.cookie );
37
38 // split the cookie into tokens using = as delimiter
39 var cookieTokens = myCookie.split( "=" );
40
41 // set name to the part of the cookie that follows the = sign
42 name = cookieTokens[ 1 ];
43 }
44 else
45 {
46 // if there was no cookie then ask the user to input a name
47 name = window.prompt( "Please enter your name", "GalAnt" );
48
54.
cookie.html
3 of 4
49// escape special characters in the name string
50 // and add name to the cookie
51 document.cookie = "name=" + escape( name );
52 }
53
54 document.writeln(
55 name + ", welcome to JavaScript programming! </h1>" );
56 document.writeln( "<a href= " JavaScript:wrongPerson() " > " +
57 "Click here if you are not " + name + "</a>" );
58
59 // reset the document's cookie if wrong person
60 function wrongPerson()
61 {
62 // reset the cookie
63 document.cookie= "name=null;" +
64 " expires=Thu, 01-Jan-95 00:00:01 GMT";
65
66 // after removing the cookie reload the page to get a new name
67 location.reload();
68 }
69
70 // -->
71 </script>
72 </head>
73
55.
74 <body>
75 <p>ClickRefresh (or Reload) to run the script again</p>
76 </body>
77 </html>
final.html
1 of 6
1<?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 12.16: final.html -->
6 <!-- Putting It All Together -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Putting It All Together</title>
11
12 <script type = "text/javascript">
13 <!--
14 var now = new Date(); // current date and time
15 var hour = now.getHours(); // current hour
16
17 // array with names of the images that will be randomly selected
18 var pictures =
19 [ "CPE", "EPT", "GPP", "GUI", "PERF", "PORT", "SEO" ];
20
21 // array with the quotes that will be randomly selected
22 var quotes = [ "Form ever follows function.<br/>" +
23 " Louis Henri Sullivan", "E pluribus unum." +
24 " (One composed of many.) <br/> Virgil", "Is it a" +
25 " world to hide virtues in?<br/> William Shakespeare" ];
58.
final.html
2 of 6
26
27// write the current date and time to the web page
28 document.write( "<p>" + now.toLocaleString() + "<br/></p>" );
29
30 // determine whether it is morning
31 if ( hour < 12 )
32 document.write( "<h2>Good Morning, " );
33 else
34 {
35 hour = hour - 12; // convert from 24 hour clock to PM time
36
37 // determine whether it is afternoon or evening
38 if ( hour < 6 )
39 document.write( "<h2>Good Afternoon, " );
40 else
41 document.write( "<h2>Good Evening, " );
42 }
43
44 // determine whether there is a cookie
45 if ( document.cookie )
46 {
47 // convert escape characters in the cookie string to their
48 // english notation
49 var myCookie = unescape( document.cookie );
50
59.
final.html
3 of 6
51// split the cookie into tokens using = as delimiter
52 var cookieTokens = myCookie.split( "=" );
53
54 // set name to the part of the cookie that follows the = sign
55 name = cookieTokens[ 1 ];
56 }
57 else
58 {
59 // if there was no cookie then ask the user to input a name
60 name = window.prompt( "Please enter your name", "GalAnt" );
61
62 // escape special characters in the name string
63 // and add name to the cookie
64 document.cookie = "name =" + escape( name );
65 }
66
67 // write the greeting to the page
68 document.writeln(
69 name + ", welcome to JavaScript programming!</h2>" );
70
71 // write the link for deleting the cookie to the page
72 document.writeln( "<a href = " JavaScript:wrongPerson() " > " +
73 "Click here if you are not " + name + "</a><br/>" );
74
60.
final.html
4 of 6
75// write the random image to the page
76 document.write ( "<img src = "" +
77 pictures[ Math.floor( Math.random() * 7 ) ] +
78 ".gif" width= " 105 " height= " 100 " /> <br/>" );
79
80 // write the random quote to the page
81 document.write ( quotes[ Math.floor( Math.random() * 3 ) ] );
82
83 // create a window with all the quotes in it
84 function allQuotes()
85 {
86 // create the child window for the quotes
87 quoteWindow = window.open( "", "", "resizable=yes, toolbar" +
88 "=no, menubar=no, status=no, location=no," +
89 " scrollBars=yes" );
90 quoteWindow.document.write( "<p>" )
91
92 // loop through all quotes and write them in the new window
93 for ( var i = 0; i < quotes.length; i++ )
94 quoteWindow.document.write( ( i + 1 ) + ".) " +
95 quotes[ i ] + "<br/><br/>");
96
61.
final.html
5 of 6
97// write a close link to the new window
98 quoteWindow.document.write( "</p><br/><a href = " " +
99 "JavaScript:window.close()">" +
100 " Close this window </a>" )
101 }
102
103 // reset the document's cookie if wrong person
104 function wrongPerson()
105 {
106 // reset the cookie
107 document.cookie= "name=null;" +
108 " expires=Thu, 01-Jan-95 00:00:01 GMT";
109
110 // after removing the cookie reload the page to get a new name
111 location.reload();
112 }
113
114 // open a new window with the quiz2.html file in it
115 function openQuiz()
116 {
117 window.open( "quiz2.html", "", "resizable = yes, " +
118 "toolbar = no, menubar = no, status = no, " +
119 "location = no, scrollBars = no");
120 }
121 // -->
62.
final.html
6 of 6
122</script>
123
124 </head>
125
126 <body>
127 <p><a href = "JavaScript:allQuotes()">View all quotes</a></p>
128
129 <p id = "quizSpot">
130 <a href = "JavaScript:openQuiz()">Please take our quiz</a></p>
131
132 <script type = "text/javascript">
133 // variable that gets the last midification date and time
134 var modDate = new Date( document.lastModified );
135
136 // write the last modified date and time to the page
137 document.write ( "This page was last modified " +
138 modDate.toLocaleString() );
139 </script>
140
141 </body>
142 </html>