Javascript And Jquery - Jon Duckett | Java Script | Cascading Style ...
Javascript and Jquery - Jon Duckett - Ebook download as PDF File (.pdf), Text File (.txt) or read book online. JavaScript Book.
-
Rating
-
Date
June 2017 -
Size
91.5MB -
Views
3,075 -
Categories
Transcript
JAVASCRIPT & JQUERY Interactive Front-End Web Development JON DUCKETT Addit ional material by: GI LLES RUPPERT & JACK MOORE WI.LEY Th.e ttxt stock is SFI ctr1il1~ Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com '')2014 by John W iley & Sons, Inc., Indianapolis, Indiana ISBN:978-l-118-53164-8 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical. photocopying, recording, scanning or otherwise, except as permitted under Sections 107or108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher. or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center. 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc.. 111 River Street. Hoboken, NJ 07030, (201) 748· 6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of W arranty: The publisher and the author make no representations or warranties w ith respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotiona l materiais. The advice and strategies contain ed herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or website may provide or recommendations it may make. Further. readers should be aware that Internet websites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762·2974. outside the United States at (317) 572·3993 or fax (317) 572- 4002. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2013933932 Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. JavaScript is a registered trademark of Oracle America, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. CREDITS For John Wiley & Sons, Inc. For Wagon Ltd. Executive Editor Carol Long Auth or Jon Duckett Cover Desig n Emme Stone Project Editor Kevin Kent Co-Authors Jack Moore (Chapters 11 & 12) Design Emme Stone Jon Duckett Gilles Ruppert (Chapter 13) Photography John Stewardson johnstewardson. com Product ion Editor Daniel Scribner Editorial M anager Mary Beth Wakefield A ssociat e Director of Marketing David Mayhew Marketing M anager Lorna Mein Business Manager Amy Knies Vice President and Executive Group Publisher Richard Swadley Associate Publisher Jim Minatel Project Coordinat or, Cover Todd Klemme Technical Review Mathias Bynens Review Team Chris Ullman David Lean Harrison Thrift Jay Bursky Richard Eskins Scott Robin Stachu Korick Thank you Annette Loudon M ichael Tomko M ichael Vella Zarb Pam Coca Rishabh Pugalia Illustration Matthew Cencich (Hotel in Chapter 3) Emme Stone (Teacher in Chapter 4) Additional Phot ography Electronics in Chapters 8 & 9: Aaron Nielsen Arkadiusz Jan Sikorski Matt Mets Mirsad Dedovic Steve Lodefink javascriptbook. com/credits This book explains how JavaScript can be used in browsers to make websites more interactive, interesting, and user-friendly. You will also learn about jQuery because it makes writing JavaScript a lot easier. To get the most out of this book, you will need t o know how to bui ld web pages using HTML and CSS. Beyond that. no prior experience with programming is necessary. Learning to program with JavaScript involves: 1 Understanding some basic programming concepts and the terms that JavaScript programmers use to describe t hem. 2 3 Learning the language itself, and, like all languages, you need to know its vocabulary and how to structure your sentences. Becoming famil iar with how it is applied by looking at examples of how JavaScript is commonly used in websites today. The only equipment you need to use this book are a computer with a modern web browser installed, and your favorite code editor, (e.g., Notepad, TextEdit. Sublime Text. or Coda). 0 INTRODUCTION CREATING OBJECTS USING LITERAL NOTATION __ .. _..,._ ____ ... ......... __.,.. ==---'"'""· --· .....,. ...,, ...__... Introduction pages come at the beginning of each chapter. They introduce the key topics you will learn about. ACCESSING ELEMENTS Reference pages introduce key pieces of JavaScript. HTML code is shown in blue, CSS code in pink, and JavaScript in green. .... , ~~~E-:;:.:~ ~·§.?=~ : .._____ -----·· -----..-·-...-..· --------·---------------- ~~;,:?~ __ _ _____ ____ ...._,._ E"":::.:::=:,,;...-==-.... ...,._ ,_....... __,.._,_..___ -------·---- ...... ..,_,,,_,._ ... .. - - -·-·-.-. . .-__ ---·--........... -..·-··-·---_.. _........___ -·---·-.. -··-·- ·-~ -----... _.........--·-··-··-- __ .. ------ - CREATING MORE OBJECT LITERALS Background pages appear on white. They explain the context of the topics covered that are discussed in each chapter. Diagram and infographics pages are shown on a dark background. They provide a simple, visual reference to topics discussed. EXAMPLE ~6.iSO"· -----··- .. ....;-••• --... ____ ___.. .. .........·--· _ .. .·--.-!-= . . ..:. :;.-_-:,.......... :-;--·... ,,....____ ·--·...-..... _ -----·--·.... ..._ ___ Example pages bring together the topics you have learned in that chapter and demonstrate how they can be applied. Summary pages come at the end of each chapter. They remind you of the key topics that were covered in each chapter. INTRODUCT I O N 0 1 ACCESS CONTENT You can use JavaScript to select any element, attribute, or tex t from an HTML page. For example: • Select the text inside all of the' +greeting
+ ' ');
8
Get the CSS and images for this example from the website that accompanies the book: www.javascriptbook. com
t_ To keep the files organized, in the same way that CSS fil es of ten live in a folder called styles or css, your JavaScript files can live in a folde r called scripts,javascript,orjs. In t his case, save your file in a folder called j s
8
THE ABC OF PROGRAMMING
add-content.htmt
• WI css !! COi.CU • QI images ..:. construalw -backdrop.Jpg :.... construalve-1090.yif • lndtx.html
..
~ jS
HTMl Folder
css Folder JPEC lmoge GIFl~t
HTML
Folder
Here you can see the file structure that you will end up with when you finish the example. Always treat file names as being case-sensitive.
LINKING TO A JAVASCRIPT FILE FROM AN HTML PAGE When you want to use JavaScript with a web page, you use the HTML tags (but it is better to put scripts in their own fil es).
Constructive & Co.
For all orders and inquiries please call 555-3344
f)
Finally, try opening the HTM L file, removing the src attribute from the opening tag. The s re attribute is no longer needed because the JavaScript is in the HTML page.
As noted on p44, it is better not to mix JavaScript in your HTM L pages like this, but it is mentioned here as you may come across this technique.
0
Open the HTML file in your web browser and the welcome greeting is written into the page.
Constructive & Co. IEStOl(E AICHITCCTUf.U MODUS
WELCOME! P'or all order•
pl..••••
and i.nqui_riea
call 555- 33''
As you may have guessed, document. write() writes content into the document (the web page). It is a simple way to add content to a page, but not always t he best. Chapter 5 discusses various ways to update the content of a page.
THE ABC OF PROGRAMMING
8
JAVASCRIPT RUNS WHERE IT IS FOUND IN THE HTML When the browser comes across a
Note how the
CUSTOM SIGNAGE:: Preview:
IM I O ~ N I T
m AIG u E ~ 1 H olu s E
Note t hat t he numbers are not written inside quotation marks. Once a value has been assigned to a variable, you can use the variable name to represent that value (much like you might have done in algebra). Here, the total cost is calculated by multiplying the price of a single tile by t he number of tiles t he customer wants. The result is then w rit ten into the page on the final two lines. You see this technique in more detail on p194 and p216. The first of these two lines finds the element whose id attribute has a value of cost, and the final line replaces the content of that element with new content. Note: There are many ways to write content into a page, and several places you can place your script. The advantages and disadvantages of each technique are discussed on p226. This technique will not work in IE8.
BASIC JAVASCRIPT IN STRUCTIONS
@
US ING A VARIABLE TO STORE A STRING For the moment, concentrate on the first four lines of JavaScript. Two variables are declared (username and message), and they are used to hold strings (the user's name and a message for that user). The code to update t he page (shown in the last four lines) is discussed fully in Chapter 5. This code selects two elements using the values of t heir id attributes. The text in those elements is updated using the values stored in these variables. Not e how the string is placed inside quote marks. The quotes can be single or double quotes, but they must match. If you start with a single quote, you must end with a single quote, and if you start with a double quote, you must end with a double quote:
0 0
0 "hello' 'hello' 0 'hello"
"hello"
Quotes should be straight (not curly) quotes:
0 1111 0 ""
0
I
I
0 ('
Strings must always be written on one line:
O
'See our upcoming range'
0 'See our upcoming range'
@ BASIC JAVASCRIPT IN STRUCTIO NS
c02/js/string-variable.js
JAVA SCRIPT
var username; var message; username = 'Molly'; message = 'See our upcoming range'; var elName = document.getElementByld{'name'); elName .textContent = username; var elNote = document .getElementByld( 'note'); elNote .textContent = message;
c02/ string-variable.html