1 of 11

Introduction to JavaScript

2 of 11

What is JavaScript?

  • Browsers have limited functionality
    • Text, images, tables, frames
  • JavaScript allows for interactivity
  • Initially Developed as a Client-Side scripting language
    • Create Dynamic Web Content in the Browser
  • Browser/page manipulation
    • Reacting to user actions
  • A type of programming language
    • Easy to learn
    • Developed by Netscape
    • Now a standard exists – �www.ecma-international.org/publications/�standards/ECMA-262.HTM

3 of 11

Java and JavaScript

  • Netscape Mozilla working with Sun (creators of Java) in the 1990’s�
  • They were developing a web-scripting language to be embedded in HTML and executed by the browser – originally called LiveScript

  • Changed name to JavaScript to foster the Sun partnership.

  • They share some common syntax and approaches but very different languages

4 of 11

JavaScript Allows Interactivity

  • Improve appearance
    • Especially graphics
    • Visual feedback
  • Site navigation
  • Perform calculations
  • Validation of input

5 of 11

How Does It Work?

  • Embedded within HTML page
    • View source
  • Executes on client
    • Fast, no connection needed once loaded
  • Simple programming statements combined with HTML tags
  • Interpreted (not compiled)
    • No special tools required

6 of 11

JavaScript Statements

<html>

<head><title>My Page</title></head>

<body>

<script language="JavaScript">

document.write('This is my first →

JavaScript Page');

</script>

</body>

</html>

Note the symbol for �line continuation

7 of 11

JavaScript Statements

<html>

<head><title>My Page</title></head>

<body>

<script language=“JavaScript">

document.write('<h1>This is my first →

JavaScript Page</h1>');

</script>

</body>

</html>

HTML written

inside JavaScript

Document.write is the JS “Method”

8 of 11

JavaScript Statements

<html>

<head><title>My Page</title></head>

<body>

<p>

<a href="myfile.html">My Page</a>

<br />

<a href="myfile.html"

onMouseover="window.alert('Hello');">

My Page</A>

</p>

</body>

</html>

JavaScript written

inside HTML

An Event

9 of 11

Example Statements

<script language="JavaScript">

window.prompt('Enter your name:','');

</script>

<form>

<input type="button" Value="Press" onClick="window.alert('Hello');">

</form>

Another event

Note quotes: " and '

10 of 11

JavaScript window.alert Method

Alert Box

  • An alert box is often used if you want to make sure information comes through to the user.
  • When an alert box pops up, the user will have to click "OK" to proceed.

Syntax

window.alert(‘sometext’);

  • The window.alert() method can be written without the window prefix.

alert(‘I am an alert box!’);

11 of 11

onerror Event

Execute a JavaScript if an error occurs when loading an image:

<img src="image.gif" onerror="myFunction()">

So – useful in XSS….

If you reference an image file that does not exist, it will run the error function/method

<img src=“NoFile.gif” onerror=alert(‘VulnerableToXSS’)>