JS
JS
JAVASCRIPT
• JavaScript is used in millions of Web pages to improve
the design, validate forms, detect browsers, create
cookies, and much more.
• JavaScript is the most popular scripting language on the
internet, and works in all major browsers, such as
Internet Explorer, Mozilla, Firefox, Netscape, Opera.
WHAT IS JAVASCRIPT?
• JavaScript was designed to add interactivity to HTML pages
• JavaScript is a scripting language (a scripting language is a
lightweight programming language)
• A JavaScript consists of lines of executable computer code
• A 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
Why Study JavaScript?
• JavaScript is one of the 3 languages all web
developers must learn:
• HTML to define the content of web pages
• CSS to specify the layout of web pages
• JavaScript to program the behavior of web pages
Are Java and JavaScript the Same?
• 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++.
JavaScript vs. PHP
• similarities:
– both are interpreted, not compiled
– both are relaxed about syntax, rules, and types
– both are case-sensitive
– both have built-in regular expressions for
powerful text processing
CS380 6
JavaScript vs. PHP
• differences:
– JS is more object-oriented
– JS focuses on user interfaces and interacting with
a document; PHP is geared toward HTML output
and file/form processing
– JS code runs on the client's browser; PHP code
runs on the web server
JS <3
CS380 7
JavaScript Capabilities
• Improve the user interface of a website
• Make your site easier to navigate
• Easily create pop-up alert, windows
• Replace images on a page without reload the
page
• Form validation
• Many others …
How to Put a JavaScript Into an HTML
Page?
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
Embedding JavaScript
<html>
<head>
<title>First JavaScript Program</title>
</head>
<body>
<script language=“JavaScript”
src=“your_source_file.js”></script>
</body>
</html>
Hide JavaScript from incompatible browsers
<script language=“JavaScript”>
<!– begin hiding JavaScript
// single-line comment, /* … */ multiple-line
comment
End hiding JavaScript -->
</script>
<noscript>
Your browser does not support JavaScript.
</noscript>
Ending Statements With a Semicolon?
• With traditional programming languages, like
C++ and Java, each code statement has to end
with a semicolon (;).
• Many programmers continue this habit when
writing JavaScript, but in general, semicolons
are optional! However, semicolons are
required if you want to put more than one
statement on a single line.
JavaScript Variables
• Variables are used to store data.
• A variable is a "container" for information you want
to store. A variable's value can change during the
script. You can refer to a variable by name to see its
value or to change its value.
• Rules for variable names:
– Variable names are case sensitive
– They must begin with a letter or the underscore character
• strname – STRNAME (not same)
Variables
• JavaScript allows you to declare and use variables to
store values.
• How to assign a name to a variable?
– Include uppercase and lowercase letters
– Digits from 0 through 9
– The underscore _ and the dollar sign $
– No space and punctuation characters
– Case-sensitive
– No reserved words or keywords
<form name="addressform">
Name: <input name="yourname"><br />
Phone: <input name="phone"><br />
Email: <input name="email"><br />
</form>
Forms and JavaScript
document.formname.elementname.value
Thus:
document.addressform.yourname.value
document.addressform.phone.value
document.addressform.email.value
Using Form Data
Personalising an alert box
<form name="alertform">
Enter your name:
<input type="text" name="yourname">
<input type="button" value= "Go"
onClick="window.alert('Hello ' + →
document.alertform.yourname.value);">
</form>
JavaScript Operators
Arithmetic Operators Operator
+
Description
Addition
Example
x=2
Result
4
(İşleçler, iki ya da daha fazla değer y=2
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
JavaScript Operators - 3
Comparison Operators Operator
==
Description
is equal to
Example
x===y returns
false
|| or x=6
y=3
(x==5 || y==5)
returns false
! not x=6
y=3
!(x==y) returns
true
JavaScript Can Change HTML Content
• One of many JavaScript HTML methods is getElementById().
• This example uses the method to "find" an HTML element (with
id="demo") and changes the element content (innerHTML) to
"Hello JavaScript":
JavaScript Can Change HTML Attributes
• This example changes an HTML image by changing the src (source) attribute
of an <img> tag:
JavaScript Can Change HTML Styles (CSS)
• Changing the style of an HTML element, is a variant of changing an HTML
attribute:
JavaScript Can Hide HTML Elements
• Hiding HTML elements can be done by changing the display style:
JavaScript Functions and Events
• A JavaScript function is a block of JavaScript code, that can be
executed when "asked" for.
• For example, a function can be executed when an event occurs,
like when the user clicks a button.
External JavaScript
• Scripts can also be placed in external files:
• External scripts are practical when the same code is used in many different
web pages.
• JavaScript files have the file extension .js.
• To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag:
External JavaScript Advantages
<script>
x=3
y=20*x+12
alert(y)
</script>
Examples -2
<script>
s1=12
s2=28
sum=s1+s2
document.write(“the sum is: "+sum)
</script>
Conditional Statements
• Very often when you write code, you want to perform different actions for
different decisions. You can use conditional statements in your code to do
this.
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Dynamic Pages
• A script can adapt the content based on explicit input from
the user or other information
– System clock: Time of day
– Hidden input
– Cookies
• User input can be collected by invoking the prompt method of
a window object
– This will display a dialog box that prompts user for input
welcome5.html
JavaScript is a loosely typed language. Variables take on any
(1 of 2)
data type depending on the value assigned.
welcome6.html
the methods of that object class
(1 of 3)
Note that conversion to integer
type was not needed when the
value was returned by the getHours
method
welcome6.html
(2 of 3)
welcome6.html
(3 of 3)