JavaScript-JQuery-Jon-Duckett New
JavaScript-JQuery-Jon-Duckett New
peach
n ee See ee em cee ee ere as ieee which elements) the Peete eee apples o, The declaration block (the aspect you want to contro, rien ee eee re cr ee RC selector andadeciaratonblock. those elements shouldappear. _forthat propery. aaa ahem clr Leet fa Sy rar tne)BROWSER SUPPORT Some early examples i this book do not work with Internet Explorer 8 and earlier (but alternative code samples that work in IE8 are available to download from http: //javascriptbook. com). We explain techniques for dealing with older browsers in later chapters. Each version ofa web browser adds new features, Often these new features make tasks easier, or are considered beter, than using older techniques But, wobsite vistors do not always keep up with the atest browser releases, so website developers cannot aay ely upon the latest technologies. ‘As you wll see, there are many inconsistencies between browsers that affect JavaScript develoners Query wll help you deal with cross-browser Inconsistencies (its one ofthe major easons why Query rapidly gained popularity amongst wed = == =) =. You need to learn to “think” like a computer because they solve tasks in different ways than you or I might approach them, Computers solve problems programmatically, they follow series of instructions, one after another. The typeof instructions they need are often cifferent to the typeof instructions you might give to another human. Therefore, thoughout th book you wil not ‘only learn the vocabulary and syntax that lavaScript uses, but you will also learn howto wt nstuctions that computers can follow. For example, when you lok tthe picture on the left how de you tll wich person s the tallest? ‘Acomputer would need expt, step-by-step Instructions, such as: 1. Find the height ofthe fst person [Link] or she the “tallest person” 3 Look atthe height ofthe remaiing people one- by-one and compare their height tothe tallest person" you have found so far 4,At each step, you find someone whose heights ‘reater than the curent “tallest person’ he or she ‘becomes the new tallest person [Link] you have checked alhe people tel me which one the tallest So the computer needst ook at each person in turn, and foreach ont performs atest Ave they taller than the curent alles! person?" Once it has done this foreach person it can give ts answer. rae asc ormocnasmns (8)DEFINING A GOAL & DESIGNING THE SCRIPT Consider how you might approach a different type of script. This example calculates the cost of a name plaque. Customers are charged by the letter. The ist thing you should do is deta your gals for the serot Ghat you wantit to schieve) Customers can have aname added toa plaque: each letter costs $5. When a ser enters aname, show them how much t wil eos Next, break tnt a saris of asks that have tobe performed n order to achieve te gals 1. The sritis triggered when the button sccked. 2tcallecs the name entered int the form fi 3: thehecks thatthe user has entered value 4. the user has not entered anything, message wil appear tling them to enter aname. 5a name hasbeen entered calculate the cost of ‘the sign by multiplying the numberof letters by the cost per letter 6. Show how much the plaque costs. (These numbers correspond with the flowchart on the iaht-hand page) CUSTOM SIGNAGE Enter name: CUSTOM SIGNAGE Enter name: rin ers nnsben ae a CUSTOM SIGNAGE Ls i HOMASSKETCHING OUT THE TASKS IN A FLOWCHART Often scripts will need to perform different tasks in different situations. You can use flowcharts to work out how the tasks fit together. The flowcharts show the paths between each step. i ‘Get he name eter int the fom t ' Ginn> @ Ccalelate the cost of he son ters rice) t ‘hoy the cout ofthe sl on te sereen ‘Arrows show how the script moves om one task tothe next. The cifferent shapes represent diferent ‘types of tasks. In some places there ave decisions which cause the code to follow eifterent paths. ‘You wil learn how to turn this exampleinto code in Chapter 2. You will also see many more examples of diferent lowchars throughout the book, and you wil meet code that helps you deal wit each of these types of situations, Some experienced programmers use move complex cancal reservations canceled |cancelBooking) | eating 4 rooms a2 werHoD what doce: cam » © | errr conser vane ot rooting roperty | gym tase ‘cancelBookingO) decreases value of bookings sroverty | boat we heckAvalabity()_sbtrct value of Bookings property ‘fom seus of rooms property ana Totus number of eam avBtaDie Ga) THE ABC OF PROGRAMMINGHOTEL OBJECT 1. When a reservations made, the book event res [Link] book event riggers the nakeBooking() method, which inreases the value ofthe bookings property 23. The vale ofthe bookings prop \yischangedto rellect how many rooms the hotel has avaiable, ‘OBJECT TYPE: CAR EVENT happens when: method call PROPERTIES brake driverslows down changespoed) | make eww GERI iver speeds up changespecso | QUIEN and closing tag for that web page, and the 1astHodi fied property ofthe dacurent object tells youthe date this page was last update,‘OBJECT TYPE: WINDOW location. http//mwwiavascripthock com/ PROPERTIES ur. hetes/wurjavascriptook com/ lastModitied 09/04/2014 15:58:57 tile Learn Javascript & jquery ‘Abook that toaches you inanicerway Se aa ne}THE DOCUMENT OBJECT REPRESENTS AN HTML PAGE Using the document object, you can access and change what content users see on the page and respond to how they interact with it Like other objects that represent real-world things, the docunent object has: PROPERTIES Properties describe characteristics of the curent web page (such as the tte ofthe page). METHODS: Methods perform asks associated with the ‘document currently leaded in the browser (such ‘8 geting information rom a specified element or ‘adding pew content) EVENTS You can respond to events, suchas a user clicking tapping onan element @ wenscor ocearne Because all major web browsersimplement the docunent abjectin the same way, the people who ‘reat the browsers hove alrend: ‘© implemented properties that you can access to find out about the curent page in the browser '© Written methods that achieve some common tasks that you are likely to want todo with an HTML page So you wil elearning how to work with this object. Infact the docunent objects just on of set of objects that all mor browsers support. When the browse ereatesamedel ofa web page, tnt oly creates a document object, butt also creates 2 new object foreach element on the page Together these objects ar described inthe Document Object ‘Model, which yu will meetin Chapter 5HOW A BROWSER SEES A WEB PAGE In order to understand how you can change the content of an HTML Pee Seg ee OL ean Pe ee seas Toa Ce eo Tad create Usa ET ar ad Perna) ead a ee Pe acre mS ag instructions (in JavaScript) into instructions the computer can follow. eee Tee aThe bro St) Lo]SUMMARY THE ABC OF PROGRAMMING B: How do computers fit in with the world around them?HOW DO! WRITE A SCRIPT FORA WEB PAGE?HOW HTML, CSS, & JAVASCRIPT FIT TOGETHER Before diving into the JavaScript language, you need to know how it will fit together with the HTML and CSS in your web pages. Web developers usually tak about tvee languages that ar ured to create web pages: HTML, CSS, and JavaScript CONTENT LAYER hte files ‘This is where the content of the page ives. The HTML gives the page structure and adds, semantics, Where posible, am to keep the ‘te languages in separates, with the HTML page linking to CSS and JavaScript fies {css} PRESENTATION LAYER ses fies ‘The CSSenhances the HTML age with ules that statehow the HTML conten is presented ackarounds, borders, box dimensions, colors fonts, te.) Programmer olten lero this 88 separation of concerns Each lanauage forms a separate layer wth citferent purpose, Each ayer, from ett right, bulds on the previous one. javascript () BEHAVIOR LAYER ‘Thisis where we can change how the page behaves, adding interactivity. We wil im to keep ‘as much of our JavaScript as possible in separate es.‘Asmore and more web-enabled








