12
JavaScript JavaScript JavaScript Introduction

JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

Embed Size (px)

Citation preview

Page 1: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

JavaScriptJavaScriptJavaScript Introduction

Page 2: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

Q. What is JavaScript?Q. What is JavaScript?Ans. JavaScript was designed to add interactivity to HTML

pages. JavaScript is a scripting language. A scripting language is a lightweight programming

language. JavaScript is usually embedded directly into HTML

pages. JavaScript is an interpreted language (means that

scripts execute without preliminary compilation). Everyone can use JavaScript without purchasing a

license.

 وقد تم تصميم جافا سكريبت إلضافة التفاعل إلى صفحاتHTMLجافا سكريبت هي لغة البرمجة

لغة البرمجة هي لغة برمجة خفيفةHTMLوعادة ما يتم تضمين جافا سكريبت مباشرة في صفحات 

جافا سكريبت هي لغة مفسرة (يعني أن تنفيذ البرامج النصية دون تجميع أولي)

ويمكن للجميع استخدام جافا سكريبت دون شراء ترخيص

Page 3: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

Q. Are Java and JavaScript is the Q. Are Java and JavaScript is the same?same?Ans.

NO!Java and JavaScript are two completely

different languages in both concept and design!

Java (developed by Sun Microsystems) is a powerful and much more complex programming language - in the same category as C and C++.

!ال

جافا سكريبت وهما لغات مختلفة تماما في كل من مفهوم وتصميم!

جافا (التي طورتها شركة صن مايكروسيستمز) هي لغة Cبرمجة قوية ومعقدة أكثر من ذلك بكثير -- في نفس فئة 

 + +.Cو

Page 4: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

Q. What is the use of JavaScript ?Q. What is the use of JavaScript ?Ans.JavaScript gives HTML designers a

programming tool .JavaScript can react to events.JavaScript can read and write HTML

elements.JavaScript can be used to validate data.JavaScript can be used to detect the

visitor's browser.JavaScript can be used to create cookies.

أداة البرمجة. HTML جافا سكريبت يعطي المصممينيمكن أن تتفاعل مع األحداث جافا سكريبت.

.HTML جافا سكريبت يمكن القراءة والكتابة عناصرويمكن استخدام جافا سكريبت للتحقق من صحة البيانات.

ويمكن استخدام جافا سكريبت لكشف متصفح الزائر..ويمكن استخدام جافا سكريبت إلنشاء ملفات تعريف االرتباط

Page 5: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

Q. Is JavaScript is same as Q. Is JavaScript is same as ECMAScript ?ECMAScript ?Ans.

JavaScript is an implementation of the ECMAScript language standard. ECMA-262 is the official JavaScript standard.

JavaScript was invented by Brendan Eich at Netscape (with Navigator 2.0), and has appeared in all browsers since 1996.

The official standardization was adopted by the ECMA organization (an industry standardization association) in 1997.

The ECMA standard (called ECMAScript-262) was approved as an international ISO (ISO/IEC 16262) standard in 1998.

هو  ECMAScript. ECMA - 262 جافا سكريبت هو تنفيذ المعيار اللغةالمعيار جافا سكريبت الرسمية.

اخترع جافا ) ، وظهرت 2.0سكريبت التي برندان إيتش في نتسكيب (مع المستكشف 

.1996في جميع المتصفحات منذ عام

اعتمد توحيد الرسمية التي يقوم  (صناعة توحيد الجمعيات) ECMA بها تنظيم .1997في عام

(ECMAScript - 262 تسمى) القياسية ECMA تمت الموافقة على1998القياسية في عام (ISO / IEC 16262) وايزو الدولية .

Page 6: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

Q. How to write JavaScript into an HTML Page / Q. How to write JavaScript into an HTML Page / Document?Document?

Examples Explained To insert a JavaScript into an HTML page, use the <script> tag. Inside the <script> tag use the type attribute to define the scripting

language. The <script> and </script> tells where the JavaScript starts and ends.

Sample Program

<html><body><h1>My First Web Page</h1>

<p id="demo">This is a paragraph.</p>

<script type="text/javascript">document.getElementById("demo").innerHTML=Date();</script>

</body></html>

The lines between the <script> and </script> contain the JavaScript and are executed by the browser.

In this case the browser will replace the content of the HTML element with id="demo", with the current date.

Page 7: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

Q. What is internal and external Q. What is internal and external JavaScript?JavaScript? Ans. JavaScript can also be placed in external files.

External JavaScript files often contain code to be used on several different web pages.

External JavaScript files have the file extension .js. Note: External script cannot contain the <script></script> tags! To use an external script, point to the .js file in the "src" attribute of the

<script> tag.

.ويمكن أيضا أن توضع في جافا سكريبت الملفات الخارجية

ملفات جافا سكريبت خارجية غالبا ما تحتوي على رمز ليتم استخدامه على عدة صفحات الويب المختلفة.

الخارجية ملفات جافا سكريبت لديها ملحق الملف. شبيبة.

العالمات!< script></ script>مالحظة : البرنامج النصي الخارجي ال يمكن أن تحتوي على <

script>الستخدام برنامج نصي خارجية، وأشر إلى الملف شبيبة. في سمة "ضمني" للعالمة <

and an Internal JavaScript which we seen in above code, that the HTML file having two tag <script> …JavaScript Code…. </script>. Which means that we can place JavaScript code in-between two tag.

Note:- JavaScripts can be put in the <body> and in the <head> sections of an HTML page.

Page 8: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

JavaScript Functions and Events (Sample JavaScript Functions and Events (Sample Program)Program)

Page 9: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

JavaScript Comments,JavaScript Comments, JavaScript Single line Comments Comments can be added to explain the JavaScript, or to make the code more readable. Single line comments start with //.Example:- <script type="text/javascript">

// Write a headingdocument.write("<h1>This is a heading</h1>");// Write two paragraphs:document.write("<p>This is a paragraph.</p>");document.write("<p>This is another paragraph.</p>");</script>

JavaScript Multi-Line Comments Multi line comments start with /* and end with */. Example:- <script type="text/javascript">

/*The code below will writeone heading and two paragraphs*/document.write("<h1>This is a heading</h1>");

</script>Using Comments to Prevent Execution<script type="text/javascript">

//document.write("<h1>This is a heading</h1>");……</script>

Page 10: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

JavaScript VariablesJavaScript VariablesYou declare JavaScript variables with

the var keyword.Like: var x; var Fname; // First method to

assign a variable.Like: var x=10; var Fname=“Mohammad”//

Assign a value to variable.It is case sensative.Variables are two types:-1. Local JavaScript Variables:- A variable

declared within a JavaScript function becomes LOCAL and can only be accessed within that function. (the variable has local scope).

2. Global JavaScript Variables:- Variables declared outside a function become GLOBAL, and all scripts and functions on the web page can access it.

Page 11: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

JavaScript OperatorsJavaScript Operators Notes:- ( = is used to assign values, + is used to add values., The + Operator Used on Strings)

Sample Program for operator)<html>

<body>

<script type="text/javascript"> JavaScript Arithmetic Operators: (Given the result y=5)

var x;

x=5+5;

document.write(x);

document.write("<br />");

x="5"+"5";

document.write(x);

document.write("<br />");

x=5+"5";

document.write(x);

document.write("<br />");

x="5"+5;

document.write(x);

document.write("<br />");

</script>

<p>The rule is: If you add a number and a string, the result will be a string.</p>

</body>

</html>

Operator

Description Example Result

+ Addition X=y+2 X=7 Y=5

- Subtraction X=y-2 X=3 Y=5

* Multiplication X=y*2 X=10 Y=5

/ Division X=y/2 X=2.5 Y=5

% Modulus (division remainder)

X=y%2 X=1 Y=5

++ Increment X=y++X=++y

X=5X=6

y=6y=6

-- Decrement X=y—X=--y

X=5X=4

Y=4Y=4

Page 12: JavaScript JavaScript Introduction. Q. What is JavaScript? Ans. JavaScript was designed to add interactivity to HTML pages. JavaScript is a scripting

JavaScript Comparison and Logical JavaScript Comparison and Logical OperatorsOperators

Comparison and Logical operators are used to test for true or false.

Comparison Operators:- Given that x=5, the table below explains the comparison operators:-

Logical Operators:- Given that x=6 and y=3, the table below.

Operator

Description Example

== Is equal to X==8 is falseX==5 is true

=== is exactly equal to (value and type)

X===5 is trueX===“5” is false

!= Not equal X!=8 is true

> Grater than X>8 is false

< Less than X<8 is true

>= Grater than or equal to X>=8 is false

<= Less than or equal to X<=8 is true

Operator Description Example

&& and (x < 10 && y > 1) is true

|| Or x==5 || y==5) is false

! Not (x==y) is true