Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.

Similar presentations


Presentation on theme: "Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server."— Presentation transcript:

1 Lesson13

2 JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.

3 JavaScript JavaScript was created by Netscape. JavaScript is easy to learn because its syntax is similar to that of the modern high- level languages like C, C++, and Java.

4 JavaScript Java and JavaScript are two separate, unrelated languages. The original name for the language was LiveScript The name JavaScript was chosen as a marketing ploy to use the name Java to attract attention.

5 JavaScript Microsoft created their own scripting language for use within a browser. It is called VBScript, and is based on Visual Basic. Fortunately, Microsoft also created JScript, their version of JavaScript. Internet Explorer can handle pages using either JavaScript (JScript) or VBScript.

6 JavaScript Jscript can be considered the same as JavaScript for practical purposes, so we can use JavaScript in both IE and Netscape. VBScript can only be used with Internet Explorer.

7 ECMAScript There is work underway to standardize the language, and a new name, ECMAscript, is used to refer to the standardized language. (ECMA stands for European Computer Manufacturer’s Association). This standardization will not affect current JavaScripts.

8 JavaScript my first script document.write("hello, world!")

9 The Document Object Model (DOM) The DOM provides a naming convention which allows us to refer to various parts of a web page by name. Once we can do this, we can use JavaScript to control and work with these different items. The DOM is not a part of JavaScript. It is a separate standard that allows us to use JavaScript with the different parts of an HTML page.

10 DOM naming conventions The DOM uses a hierarchical naming convention, like Java. We break the page up into a number of objects. We use dots to separate the different levels, from general to specific.

11 DOM naming conventions This is similar to specifying where someone lives by starting with the continent, then the country, then the province, then the city, then the street, then the building number, then the apartment, like this; NorthAmerica.Canada.Ontario.Toronto.AnyStreet.54.305

12 DOM naming conventions Some DOM objects are: –document: refers to the HTML page –window: refers to the browser window –forms: refers to the form elements in the document (e.g. text boxes, radio buttons etc.)

13 DOM naming conventions Some more DOM objects are: –images: refers to all the images in a document –navigator: refers to an unseen object that contains information about the browser version and type.

14 Events For our purposes at this time, an event is the result of an action taken by a user. Examples of user actions are; –Moving the mouse cursor over an image –Moving the mouse cursor away from an image –Clicking the mouse button –Double clicking the mouse button

15 Events Certain HTML tags have attributes to describe these events. These are called event handlers. The browser will take action when an event occurs, based on the value of the attribute.

16 Events Some common event handlers we will use are: –onMouseOver : Defines the action to take when the mouse pointer is placed over the image. –onMouseOut: Defines the action to take when the mouse pointer is moved off the image.

17 Events More common event handlers: –onChange: Defines an action to take when there is a change to the input field. –onBlur: Defines an action to take when focus is removed from the element.

18 Events More common event handlers: –onSubmit: Defines the action to take when a form has been submitted. –onLoad: Defines an action to take when a page has loaded.

19 JavaScript (continued) my first script <!-- hide script from old browsers document.write("hello, world!") // end hiding script from old browsers -->

20 JavaScript (continued) my first script <!-- hide script from old browsers /* this is an example of a long javascript comment. note the characters at the beginning and ending of the comment. this script adds the words "hello, world!" into the body area of the html page. */ document.write("hello, world!") // end hiding script from old browsers -->

21 JavaScript (continued) you should have javascript! <!-- hide script from old browsers window.location="jswelcome.html" // end hiding script from old browsers --> you won't get much from this site without the latest version of javascript--i suggest that you upgrade now!

22 JavaScript (continued) welcome to our site <a href="script04.html" onclick="window.location='jswelcome.html'; return false">welcome to our site... c'mon in!

23 JavaScript (continued) welcome to our site <a href="script04.html" onclick="window.location='jswelcome.html'; return false">welcome to our site... c'mon in!

24 JavaScript (continued) what's your browser? <!-- hide script from old browsers if (navigator.appname == "netscape") { document.write("you're running netscape navigator, a fine javascript- enabled browser.") } else { document.write("you're not running netscape navigator--maybe you should?") } // end hiding script from old browsers -->

25 Dynamic images (1) For our first use of DHTML, we will learn how to cause an image to change, based on the mouse position. This is commonly referred to as an image flip.

26 Dynamic images (2) The tag has the following attributes that we will take advantage of; –onMouseOver : Defines the action to take when the mouse pointer is placed over the image which is inside the tag. –onMouseOut: Defines the action to take when the mouse pointer is moved off the image which is inside the tag.

27 Dynamic images (3) We will make it so that when the user puts the mouse over a certain image, that image changes. To do this, we need to create the two different images. 1.The default image, the one the user sees first. 2.The swap image, the one that replaces the default image when the mouse is placed over it.

28 Dynamic images (4) The default image: The swap image:

29 Dynamic images (5) Start with a normal tag inside an anchor, like this; Give a name to our image

30 Dynamic images (6) In order to change the image on the screen, we need to be able to refer to it. We use the DOM for this, giving the complete path to the image. The DOM path to our image is document.images.image1

31 Dynamic images (7) Now we specify what we want to change about this image, in this case it is the src attribute (i.e. which image is being displayed) document.images.image1.src

32 Dynamic images (8) Next we add the onMouseOver attribute to the tag containing the image we want to change, and we use the DOM to say what image to use instead. Note the single quotes inside the double quotes.

33 Dynamic images (9) This works fine, but once we put the mouse over the image, it changes and stays that way. Now we want to make it so that when we move the mouse off the image, it goes back to the original image.

34 Dynamic images (10) Now we need to use the onMouseOut attribute to set the behaviour when the mouse cursor is moved off the image.

35 Dynamic images (11) We just have to change the code as follows; <a href="mouseovermouseout.html" onMouseOver="document.images.image1.src='presshere.gif'" onMouseOut="document.images.image1.src='lookatme.gif'">

36 JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide error messages which can be very helpful in debugging JavaScript.


Download ppt "Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server."

Similar presentations


Ads by Google