cmpt 165 - github pages · cmpt 165 introduction to the internet and the world wide web by unit4:...

13
CMPT 165 CMPT 165 INTRODUCTION TO THE INTERNET INTRODUCTION TO THE INTERNET AND THE WORLD WIDE WEB AND THE WORLD WIDE WEB By UNIT4: INTRO TO JAVASCRIPT UNIT4: INTRO TO JAVASCRIPT Hassan S. Shavarani 1

Upload: others

Post on 25-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

CMPT 165CMPT 165INTRODUCTION TO THE INTERNET INTRODUCTION TO THE INTERNET

AND THE WORLD WIDE WEBAND THE WORLD WIDE WEBBy

UNIT4: INTRO TO JAVASCRIPTUNIT4: INTRO TO JAVASCRIPT

Hassan S. Shavarani

1

Page 2: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

TOPICSTOPICS

4. The jQuery Library

1. Programming and Web Pages2. JavaScript Basics3. Variables and Functions

5. Working with jQuery6. Events and Behaviour7. Why doesn't my code work?

2

Page 3: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

LIBRARIES IN PROGRAMMINGLIBRARIES IN PROGRAMMING

JavaScript is full-featured all by itself but there are many tasks for which a lot needs to be

done and are not in the core language

3

Page 4: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

COMMON ATTRIBUTE OF PROGRAMMING LANGUAGESCOMMON ATTRIBUTE OF PROGRAMMING LANGUAGESthe language itself lets programmers solve problems,

but as the language is used, it becomes clear thatsome tasks must be done often

4

Page 5: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

it would be inefficient for every programmer, or eveneach project/company to solve these problems

separately !!!

developers often publish their solutions to frequently-encountered problems in code libraries (or just

libraries)

5

Page 6: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

LINKING LINKING jQueryjQuery

immediately upon importing jquery-3.2.1.js, thevariable jQuery will be accessible!

<head>     <meta charset="UTF-8" />     <title>Page with a style sheet</title>     <script src="http://cmpt165.csil.sfu.ca/                      js/jquery-3.2.1.js" /> </head>

let's take a look at an HTML page linked to jquery-3.2.1.js containing an alert function call here

6

Page 7: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

LET'S GET STARTED WITH LET'S GET STARTED WITH jQueryjQuerysetup = function() {     all_paragraphs = jQuery('p')     all_paragraphs.click(say_hello) } jquery_doc = jQuery(document) jquery_doc.ready(setup)

The string "p" given to the jQuery function is a selector (or jQuery selector to be morespecific)

7

Page 8: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

jQueryjQuery SELECTORS SELECTORSdesigned to work like CSS selectors

because most developers already know how to use CSS selectors

8

Page 9: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

jQueryjQuery SELECTORS VS. CSS SELECTORS SELECTORS VS. CSS SELECTORSThere are some differences between CSS selectors and jQuery selectors

but for now you can safely assume they are thesame

* The differences are mostly that jQuery extends things so you can specify things injQuery that won't work in CSS, for more details look at here

9

Page 10: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

After calling the jQuery selector, what it gives backis a jQuery object

10

Page 11: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

MODIFYING THE PAGE EXAMPLEMODIFYING THE PAGE EXAMPLEp_click = function() {     jQuery('#changeme').html('Somebody clicked me.') } h1_hover = function() {     jQuery('#changeme').html('Mouse <em>over</em> the&lt;h1&gt;.') } setup = function() {     jQuery('#changeme').click(p_click)     jQuery('h1').mouseover(h1_hover) } jQuery(document).ready(setup)

* To try this code look at here

11

Page 12: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

EVENTSEVENTSAn event in HTML is triggered by certain things thatthe user does, or that happens in the browser, lets

look at some event categories:

* we can see how to attach an event handler in JavaScript code if not using jQuery event attaching mechanism

* you can find a reference to the Event Object descriptions

Mouse Events CategoryKeyboard Events CategoryBrowser Events CategoryDocument Loading Functions Category

here

here

12

Page 13: CMPT 165 - GitHub Pages · cmpt 165 introduction to the internet and the world wide web by unit4: intro to javascript hassan s. shavarani 1. topics 4. the jquery library d tsphsbnnjoh

Any Questions?

13