WS Lec7 Xxxxxxxxxx& HTML 3
WS Lec7 Xxxxxxxxxx& HTML 3
Strings
● Lines 11–12 instruct the browser’s JavaScript interpreter to perform an action,
namely, to display in the web page the string of characters contained between
the double quotation (") marks.
● Consecutive spaces appear in a string will be interpreted as single space.
● The term object normally implies that attributes (data) and behaviors
(methods) are associated with the object.
● The object’s methods use the attributes to perform useful actions for the client
of the object.
● In lines 11–12, we call the document object’s writeln method to write a line of
HTML5 markup in the HTML5 document.
Statements
● Every statement in JavaScript ends with semicolon (;).
● Line 14 indicates the end of the script.
Note: JavaScript code is typically placed in a separate file, then included in the
HTML5 document that uses the script. This makes the code more reusable, because
it can be included into any HTML5 document.
Web Systems [502315-3] Dr. Afnan Bukhari 16
First Script (cont.)
Identifiers
● The name of a variable can be any valid identifier.
● An identifier is a series of characters consisting of letters, digits, underscores ( _ ) and
dollar signs ($) that does not begin with a digit and is not a reserved JavaScript keyword.
● Identifiers may not contain spaces.
Case Sensitivity
● JavaScript is case sensitive—uppercase and lowercase letters are considered to
be different characters, so name, Name and NAME are different identifiers.
Web Systems [502315-3] Dr. Afnan Bukhari 21
JavaScript Comments
JavaScript Comments
● In line 11, a single-line comment that begins with the characters //
states the purpose of the variable in the script.
● Comments do not cause the browser to perform any action when the
script is interpreted; rather, comments are ignored by the JavaScript
interpreter.
Multiline Comments
● You can also write multiline comments. For example,
/* This is a multiline
comment. It can be
split over many lines. */
is a multiline comment spread over several lines.
● A script can adapt the content based on input from the user or other variables,
such as the time of day or the type of browser used by the client.
● Such web pages are said to be dynamic, as opposed to static, since their content
has the ability to change.
String Concatenation
● Lines 16–17 use document.writeln to display the new welcome message.
● The expression inside the parentheses uses the operator + to “add” a string
("<h1>Hello, "), the variable name and another string (", welcome to
JavaScript programming!</h1>").
● JavaScript has a version of the + operator for string concatenation that enables
a string and a value of another data type (including another string) to be
combined.
● As you’ll see later, the + operator used for string concatenation can convert other
variable types to strings if necessary. Because string concatenation occurs
between two strings, JavaScript must convert other variable types to strings
before it can proceed with the operation.
Web Systems [502315-3] Dr. Afnan Bukhari 25
Operators & Statments
Adding Integers
● Our next script illustrates another use of prompt dialogs to obtain input from the
user. Figure 6.7 inputs two integers (whole numbers, such as 7, –11, 0 and 31914)
typed by a user at the keyboard, computes the sum of the values and displays the
result.
● Lines 11–15 declare the variables firstNumber, secondNumber, number1, number2
and sum.
● The script assigns the first value entered by the user to the variable firstNumber.
● Line 21 displays a prompt dialog to obtain the second number to add and assigns
this value to the variable secondNumber.
● Lines 24–25 convert the two strings input by the user to integer values that can be
used in a calculation. Function parseInt converts its string argument to an integer.
10. <script>
11. // Function definition
12. function addNumbers(a, b) {
13. return a + b;
14. }
19. </script>
20. </body>
21. </html>
11. <script>
12. // JavaScript: Event Handling
13. document.getElementById("myButton").addEventListener("click",
function() {
14. document.getElementById("message").innerText = "Text changed
after button click!";
15. });
16. </script>
17. </body>
18. </html>
15. <script>
16. function changeColor() {
17.
document.getElementById("text").style.color = "red";
18. }
● You can attach event handlers to HTML elements with very little
knowledge of the DOM
● However, to change what is displayed on the page requires
knowledge of how to refer to the various elements
● The basic DOM is a W3C standard and is consistent across various
browsers
○ More complex features are browser-dependent
● The highest level element (for the current page) is window, and
everything else descends from that
Source:
https://www.technologyuk.net/comput
ing/website-development/world-wide-
web/document-object-model.shtml
● window
○ The current window (not usually needed).
● self
○ Same as window.
● parent
○ If in a frame, the immediately enclosing window.
● top
○ If in a frame, the outermost enclosing window.
● frames[ ]
○ An array of frames (if any) within the current window. Frames are
themselves windows.
● document
○ The HTML document being displayed in this window.
● location
○ The URL of the document being displayed in this window. If you set this
property to a new URL, that URL will be loaded into this window. Calling
location.reload() will refresh the window.
● navigator
○ A reference to the Navigator (browser) object. Some properties of
Navigator are:
■ appName -- the name of the browser, such as "Netscape"
■ platform -- the computer running the browser, such as "Win32"
● alert(string)
○ Displays an alert dialog box containing the string and an OK button.
● confirm(string)
○ Displays a confirmation box containing the string along with Cancel and OK
buttons. Returns true if OK is pressed, false if Cancel is pressed.
● prompt(string)
○ Displays a confirmation box containing the string, a text field, and Cancel
and OK buttons. Returns the string entered by the user if OK is pressed, null
if Cancel is pressed.
● open(URL)
○ Opens a new window containing the document specified by the URL.
● close()
○ Closes the given window (which should be a top-level window, not a frame).
Web Systems [502315-3] Dr. Afnan Bukhari 57
20. // Confirm using window.confirm
1. <!DOCTYPE html> 21. function showConfirm() {
2. <html lang="en"> 22. const result = window.confirm("Do you want to proceed?");
3. <head> 23. window.document.getElementById("output").textContent =
4. <meta charset="UTF-8"> "Confirm result: " + result;
24. }
5. <title>JavaScript Window and Dialog
Functions</title> 25. // Prompt using window.prompt
6. </head> 26. function showPrompt() {
7. <body> 27. const name = window.prompt("What is your name?");
28. if (name !== null) {
8. <h1>JavaScript Window and Dialog Functions</h1> 29. window.document.getElementById("output").textContent = "Hello,
" + name + "!";
9. <button onclick="showAlert()">Show Alert</button> 30. } else {
10. <button onclick="showConfirm()">Show 31. window.document.getElementById("output").textContent =
"Prompt was canceled.";
Confirm</button> 32. }
11. <button onclick="showPrompt()">Show 33. }
Prompt</button>
12. <button onclick="openWindow()">Open New 34. // Open and close window using window.open and window.close
35. let newWindow;
Window</button> 36. function openWindow() {
13. <button onclick="closeWindow()">Close New 37. newWindow = window.open("https://example.com", "_blank",
Window</button> "width=400,height=300");
38. }
14. <p id="output"></p> 39. function closeWindow() {
40. if (newWindow) {
15. <script> 41. newWindow.close();
16. // Alert using window.alert 42. } else {
43. window.alert("No window is open.");
17. function showAlert() { 44. }
18. window.alert("This is an alert box!"); 45. }
19. } 46. </script>
47. </body>
48. </html>
Web Systems [502315-3] Dr. Afnan Bukhari
Thanks!
Bukhari.a@tu.edu.sa
59