JavaScript: Introduction to ScriptingJavaScript: مقدمة في البرمجة النصية

الشريحة 1 من 19

مقدمة الفصل

تفاصيل المصدرالفصل 7 · الشريحة ⁦3⁩
المصدر
الفصل 7 · الشريحة ⁦3⁩

Client-side and server-side scriptingالعميل والخادم

Client-side vs. server-side scripting and JavaScript

Client sideجهة العميل

  • Runs in the web browser used to view the pages
  • Executed on local computers after the pages are loaded
  • JavaScript

Server sideجهة الخادم

  • Runs on the server that hosts the website
  • The browser cannot understand PHP — only JavaScript, HTML (the structure of the page) and CSS.
  • JavaScript is a scripting language used to enhance the functionality and appearance of web pages; it interacts with the page through events (e.g., calculators).
  • Scripts can be embedded in web pages or kept in a separate file.
تفاصيل المصدرالفصل 7 · الشرائح ⁦4–6⁩
المصدر
الفصل 7 · الشرائح ⁦4–6⁩

سؤال سريع

Client-side vs. server-side scripting and JavaScript

A page has a calculator that updates the total as soon as the user types, without contacting the server. This is an example of:

تفاصيل المصدرالفصل 7 · الشرائح ⁦4–5⁩
المصدر
الفصل 7 · الشرائح ⁦4–5⁩

BOM and DOMBOM و DOM

The Browser Object Model and the Document Object Model

Browser Object Model (BOM)
Used to interact with the browser; its properties and methods let JavaScript interact with the web browser.

Important BOM objects

  • document
  • location
  • history
  • navigator
  • screen
  • frames
Document Object Model (DOM)
Deals with the document — the HTML elements themselves — organized in a tree structure (DOM tree). Every element is an object (tree node) with properties and methods; the browser creates the DOM tree when the page is loaded.
مخطط الشريحة 9

Document

Root element: <HTML>

Element: <HEAD>

Element: <TITLE>

Text: "Your Title"

Element: <SCRIPT>

Attribute: "SRC"

Element: <BODY>

Element: <DIV>

Text: "Your Div Text"

Element: <P>

Text: "Your Para Text"
تفاصيل المصدرالفصل 7 · الشرائح ⁦7–9⁩
المصدر
الفصل 7 · الشرائح ⁦7–9⁩

سؤال سريع

The Browser Object Model and the Document Object Model

Which statement correctly contrasts the BOM and the DOM?

تفاصيل المصدرالفصل 7 · الشرائح ⁦7–8⁩
المصدر
الفصل 7 · الشرائح ⁦7–8⁩

Your first scriptأول سكربت

  • All major web browsers contain JavaScript interpreters.
  • JavaScripts often appear in the <head> section — the browser interprets the head first.
  • The <script> tag indicates that the text which follows is part of a script; type gives the MIME type and language (e.g., text/javascript) and may be omitted in HTML5.
  • Text between double quotation marks is a string literal.

من الشرائح

  • <script type = "text/javascript"> document.writeln( "<h1>Welcome to JavaScript Programming!</h1>" ); </script>
تفاصيل المصدرالفصل 7 · الشرائح ⁦10–14⁩
المصدر
الفصل 7 · الشرائح ⁦10–14⁩
ملاحظات حول المصدر (1)
  • تقول الشريحة 13 إن النوع الافتراضي للسكربت في HTML5 هو text/html، ويبدو خطأً في الشريحة.

Objects, writeln and writeالكائنات و writeln و write

The document object, methods, writeln and write

Object
Resides in memory and contains information used by the script: attributes (data) and behaviors (methods). The document object represents the HTML5 document being displayed.
  • The parentheses after a method name contain its arguments.
  • Every statement should end with a semicolon (statement terminator), although none is required.
  • JavaScript is case sensitive — wrong case is a syntax error.
  • JavaScript is typically placed in a separate file to make it more reusable.

writeln

  • Writes a line of HTML5 text in the document

write

  • Displays a string like writeln
  • Does not move the output cursor to the beginning of the next line
تفاصيل المصدرالفصل 7 · الشرائح ⁦15–19⁩
المصدر
الفصل 7 · الشرائح ⁦15–19⁩

سؤال سريع

The document object, methods, writeln and write

Which statement about document.write and document.writeln is correct?

تفاصيل المصدرالفصل 7 · الشرائح ⁦17, 19⁩
المصدر
الفصل 7 · الشرائح ⁦17, 19⁩

Concatenation, alert and escape sequencesالضم و alert والهروب

Concatenation, alert dialogs and escape sequences

+ (concatenation operator)
Joins two strings together.
window.alert
Displays an alert dialog that pops up to grab the user's attention; its argument is the string to display.
Escape sequence
A backslash (\) combined with the next character in a string; \n is the newline character.
Fig. 6.4 — Some common escape sequences
Escape sequenceالتسلسلDescription (Fig. 6.4)الوصف
\nNew line — position the cursor at the beginning of the next line
\tHorizontal tab — move the cursor to the next tab stop
\\Backslash — represent a backslash character in a string
\"Double quote — a double-quote character in a string contained in double quotes
\'Single quote — a single-quote character in a string
تفاصيل المصدرالفصل 7 · الشرائح ⁦21, 22, 24, 25⁩
المصدر
الفصل 7 · الشرائح ⁦21, 22, 24, 25⁩

سؤال سريع

Concatenation, alert dialogs and escape sequences

Which call shows an alert whose text is: Say "hello"

تفاصيل المصدرالفصل 7 · الشريحة ⁦25⁩
المصدر
الفصل 7 · الشريحة ⁦25⁩

prompt and variablesprompt والمتغيرات

prompt dialogs, variables and parseInt

Scripting generates page content when it is shown — such pages are dynamic, as opposed to static.

window.prompt
Displays a dialog into which the user can type a value. First argument: the message; optional second argument: the default string in the text field.
Variable
A location in memory where a value can be stored. It has a name, type and value and should be declared with var.
Identifier
Letters, digits, underscores (_) and dollar signs ($); does not begin with a digit; not a reserved keyword.
Comments
// single-line; /* … */ multiline. Ignored by the interpreter.
تفاصيل المصدرالفصل 7 · الشرائح ⁦26, 29, 30, 31⁩
المصدر
الفصل 7 · الشرائح ⁦26, 29, 30, 31⁩

سؤال سريع

prompt dialogs, variables and parseInt

var city = window.prompt( "Enter your city", "Riyadh" ); What is "Riyadh" here?

تفاصيل المصدرالفصل 7 · الشريحة ⁦31⁩
المصدر
الفصل 7 · الشريحة ⁦31⁩

Assignment, null and parseIntالإسناد و null و parseInt

prompt dialogs, variables and parseInt

= (assignment operator)
Assigns a value to a variable. It is a binary operator (two operands).
null
Signifies that a variable has no value. It is not a string literal; writing it to the document displays the word "null".
parseInt
Converts its string argument to an integer.
تفاصيل المصدرالفصل 7 · الشرائح ⁦32–34⁩
المصدر
الفصل 7 · الشرائح ⁦32–34⁩

سؤال سريع

prompt dialogs, variables and parseInt

var a = "4"; var b = "5"; Which expression gives the number 9?

تفاصيل المصدرالفصل 7 · الشريحة ⁦33⁩
المصدر
الفصل 7 · الشريحة ⁦33⁩

Arithmeticالعمليات الحسابية

  • +, -, *, / and % are binary operators.
  • The remainder operator % yields the remainder after division.
  • Arithmetic expressions are written in straight-line form; parentheses group expressions.
Fig. 6.12 — Precedence of arithmetic operators
Operator(s)العاملOperation(s)العمليةOrder of evaluation (Fig. 6.12)ترتيب التقييم
*, / or %Multiplication, Division, Remainder
  • Evaluated first
  • several such operations are evaluated left to right
+ or -Addition, Subtraction
  • Evaluated last
  • several such operations are evaluated left to right
تفاصيل المصدرالفصل 7 · الشرائح ⁦37–39⁩
المصدر
الفصل 7 · الشرائح ⁦37–39⁩

سؤال سريع

Arithmetic and operator precedence

What is the value of 10 - 4 / 2 + 3 % 2 in JavaScript?

تفاصيل المصدرالفصل 7 · الشرائح ⁦38–39⁩
المصدر
الفصل 7 · الشرائح ⁦38–39⁩

Decision making with ifجملة if

Decision making: if, equality and relational operators, Date

if statement
Makes a decision based on the truth or falsity of a condition: if true, the body executes; if false, it does not.
Fig. 6.13 — Equality and relational operators
AlgebraicجبريJavaScriptJavaScriptSampleمثالMeaningالمعنى
===x == yx is equal to y
≠!=x != yx is not equal to y
>>x > yx is greater than y
<<x < yx is less than y
≥>=x >= yx is greater than or equal to y
≤<=x <= yx is less than or equal to y
  • Equality operators have the same precedence, lower than the relational operators; they associate left to right.
  • The Date object acquires the current local time: new Date().
تفاصيل المصدرالفصل 7 · الشرائح ⁦40, 41, 42, 46⁩
المصدر
الفصل 7 · الشرائح ⁦40, 41, 42, 46⁩

سؤال سريع

Decision making: if, equality and relational operators, Date

Which if statement displays "Adult" only when age is 18 or more?

تفاصيل المصدرالفصل 7 · الشرائح ⁦40, 42⁩
المصدر
الفصل 7 · الشرائح ⁦40, 42⁩

الخلاصة

تفاصيل المصدر
المصدر
الفصل 7

خريطة الفصل

    1. 1مقدمة الفصل
  1. Client-side vs. server-side scripting and JavaScriptالبرمجة في جهة العميل والخادم و JavaScript

    1. 2Client-side and server-side scriptingالعميل والخادم
    2. 3سؤال سريع: جهة العميل
  2. The Browser Object Model and the Document Object Modelنموذج كائنات المتصفح ونموذج كائنات المستند

    1. 4BOM and DOMBOM و DOM
    2. 5سؤال سريع: DOM
  3. Your first script: the script elementأول سكربت: العنصر script

    1. 6Your first scriptأول سكربت
  4. The document object, methods, writeln and writeالكائن document والطريقتان writeln و write

    1. 7Objects, writeln and writeالكائنات و writeln و write
    2. 8سؤال سريع: write
  5. Concatenation, alert dialogs and escape sequencesالضم ومربع التنبيه وتسلسلات الهروب

    1. 9Concatenation, alert and escape sequencesالضم و alert والهروب
    2. 10سؤال سريع: الهروب
  6. prompt dialogs, variables and parseIntمربع prompt والمتغيرات و parseInt

    1. 11prompt and variablesprompt والمتغيرات
    2. 12سؤال سريع: prompt
    3. 13Assignment, null and parseIntالإسناد و null و parseInt
    4. 14سؤال سريع: parseInt
  7. Arithmetic and operator precedenceالعمليات الحسابية وأسبقيتها

    1. 15Arithmeticالعمليات الحسابية
    2. 16سؤال سريع: الأسبقية
  8. Decision making: if, equality and relational operators, Dateاتخاذ القرار: if وعمليات المساواة والعلاقة و Date

    1. 17Decision making with ifجملة if
    2. 18سؤال سريع: if
    1. 19الخلاصة