klo fliesen ideen
david j. malan: all right,so this here is the myo arm band, a couple of which wehave for cs50 final projects. and that was a demonstration we queuedyou up in advance where essentially this fairly tight arm band up herelistens to your muscular movements that are then mapped in softwareto colton's laptop over here which had itunes and thatsong already queued up. rather than me demoing this,colton's been in the lab clearly all week getting a demonstrationready for one brave volunteer. if someone would like to comeon up-- saw your hand first.
come on up. >> all right. and what is your name? >> audience: uh, maria. >> david j. malan: maria, nice to see you. come on over here. let me introduce you to colton. colton, this is maria. >> colton: hi, nice to meet you.
>> david j. malan: allright, so step one, we're going to have you putthis on to your forearm so that it's prettytight up near your elbow. and meanwhile, let's haveput on our google glass and we'll mix technologies today. >> colton: first we'll have tohook this into the things. >> david j. malan: ok. actually, let's put your arm asclose to this cable as possible so that we can first sync it up.
>> colton: let's do this. >> david j. malan: and meanwhile, sothat everyone can get a closer glance, we'll toss andrew's cameraup on the screen there. so we have a usb cable that'sbeing plugged into maria's armband. and let me toss colton's screenup on the projector next. >> so colton is registering the devicenow as a myo connected to this cable. and now what maria'sgoing to do momentarily is actually walk throughthe calibration steps and teach the softwarehow her muscles respond
when she make certain pre-definedgestures that the software understands. if you'd like to go infront of the screen. ok, keep trying. >> colton: go like this. and like that. and all the way to the right. go back. different perspective. it's not you.
it's us. >> maria: ok. david j. malan: no. let's move it higher up so it'scloser to your elbow, or even tighter. all right. >> here we go. this would be a good time for cs52x. there we go. >> very nice.
ok. thumb to pinky. spread your fingers. good. wave right. it's curiously showingyou with the left hand-- >> colton: yeah, that's wierd. david j. malan: wave tothe right and move forward. fast forward to skip or next.
that's ok wave right. >> maria: i don't-- wait. >> david j. malan: need some help? >> colton: so you're going like this. maria: it's turning theother thing, though. colton: it is. david j. malan: yeah i don't knowwhy it's showing you a leftie. colton: why don't you try--just try going like this. >> david j. malan: no?
maybe reach your armout a little straighter and make it more abrupt like this. yeah, ok, come on. >> maria: i'm sorry. david j. malan: it's not your fault. colton: it's fine. david j. malan: all right. well-- >> maria: should we skip this, then?
david j. malan: yes, let'slet you off the hook. so if anyone would like to do afinal project using this cutting edge hardware, realize it might justtake a little getting used to. and this-- the reality is thisis actually very bleeding edge. >> this is what's calledthe developer kit, which is meant to be essentially a pre-releaseso that people can do exactly this-- fight with it, figureout how people's bodies work with the technology. so if you wantafterwards, after lecture,
we can let you come andtake another stab at it. but otherwise, a round of applause, ifwe could, for maria for coming on up. >> maria: thank you. >> david j. malan: thank you. we'll hang on to this, but we will giveyou-- how about a stress ball here? oh, and-- if-- yeah, thanks. so for the curious, if you wereunfamiliar with the sound choice that we made thereearlier, an amazing tv show that you should absolutelybe binge-watching on netflix
is this one here. >> speaker 1: ladies and gentlemen,a magician named josh. >> david j. malan: and apparently, it'sa thing to text me during lecture now. i'm being told that mariahad a birthday yesterday. so happy birthday fromcs50 to maria as well. >> so you may have read in recent monththat this gentlemen here, steve ballmer, who was actuallyclass of 1977 at the college, recently retired for microsoft. he was an undergraduate here,then a couple years later
found himself atstanford business school when he received a phonecall from a friend of his who had lived down the hallfrom him here at harvard. that friend's name was billgates, and at the time, he was trying to recruit steve to bethe first business person, really, at a small company name microsoft. >> a long story short, stevewas ultimately won over, joined microsoft when theyhad just 30 employees. and by the time heretired quite recently,
the company had 100,000 employeesover the past few years. and so a website known as the vergeprepared this tribute on video that we thought we'dshared that gives you a sense of just how much energy stevebrings to any presentation he gives. [video playback] -microsoft's like a fourth child. children do leave the house. in this case, i guessi'm leaving the house. hey bill, whazzap?
>> -wazzap? >> -hey, wazzap? we've been given anenormous opportunity. and bill gave us that opportunity. i want to thank bill for that. i want you to too. the pace of innovationisn't going to slow. >> it's going to get faster and faster. there might be a few competitorsthat are unfortunately eliminated!
>> i love this company. yes! i am a pc, and i love this company! >> developers, developers, developers,developers, developers, developers, developers, developers. web developers! >> web developers! listen to what else youget at no extra charge! >> the ms-dos executive, an appointmentcalender, a card pile, a note pad,
a clock, a control panel. and, can you believe it? reversie! >> burn them to cd! post them to msn! you mail them to friends! >> all with one click! one microsoft, one strategy, one team--focused, disciplined, professional, and expert in all that we do.
let me use a line from an old movie. >> relationships are like sharks. they move forward or they die. i actually think techcompanies are the same. >> [end video playback] david j. malan: so we are so pleased toannounce that steve will be joining us here in cs50 next wednesday atthe usual place and time here. space will likely be limited. and so to join us in person, pleasehead today or shortly thereafter
to cs50.harvard.edu/register. >> and we will follow up bytuesday confirming spots. look forward to that nextwednesday during lecture in cs50. now, in other news, i happened tocome across this in the crimson just the other day. >> it turns out that one of cs50's staffand at least one of cs50's students is currently running for ucpresident and vice president, which brought me backto my own days back when i lost the uc election miserably.
but the silver liningin that is i always tell the story is thatone of the i'm sure many reasons i lost theelection was a complete lack of a talent for public speaking. and so quite honestly, itdrove me, that experience i think my junior year, to actually signup for harvard computer society, which is the group on campus thatholds various technical talks and other things. and i took over their teachingseminars and therefore
had an opportunity, awonderful opportunity, to start working on exactly this. but also, i had an opportunityduring this experience to teach myself all the more html. and so i procrastinated last night bylooking through the html based website i made in like 1997, '98, for mycampaign which looks like this here. i know. >> because-- and of course, noticethis amazing design decision in 1998 or whatnot.
the first thing you want usersto do upon visiting your website is to have to click another link justto enter your website here with the monk behind like a shrouded curtain whereapparently my campaign platform was. and this is all you'll gettoday is just a screenshot. but i was reading through, like,my campaign posters last night and my platform. >> and i was so angry at the time. my platform was-- it was interesting. so i've calmed down since then.
but someday, i will run again andhopefully better off this time. >> so html, that language in which i madethat in-- you'll soon make much more-- is something we've beentalking about of late and largely taking for granted nowthat we've moved on to other languages. but let's pause for just a moment andput some of these things in context. so in a sentence, what is html? >> or, what is used for? anyone? yes.
>> audience: markup for websites. david j. malan: markup for website. so it's a markup language thatlets you structure a web page. header goes up here, titlegoes here, body goes here. this is boldface, this isitalics-- that sort of detail. >> ok, good. so css lets you-- and itook some liberties there with the bold-facing and italics becausethat's better implemented with this. css is-- what?
say in a sentence. anyone at all. yeah. >> audience: embellishments andstuff, like how to design it. david j. malan: ok, good. embellishments that allow youto design it or stylize it with things like boldface anditalics and colors and also more fine grained positioning of elements. it sort of lets you take things thelast mile so that if, for instance,
in pset7, you might have noticed on yourportfolio page if you're at this point already that a default table that youmake to show the user's stock holdings and cash probably looks pretty hideousby default with no white space. everything's kind of crammedtogether in rows and columns. >> well, with a bit ofcss, as you may realize, you can actually tweak that and make itsomething much more familiar and much prettier to look at. so css is about thestylization of websites. but then we introduced yet anotherlanguage, php, which lets us do what?
>> let's just do what? anyone. got to venture beyondthe first couple rows. >> audience: generate dynamic content. david j. malan: perfect. generate dynamic content. and you can do this inany number of languages. we happen to use php because it'sin part so similar to c syntax. >> but php does exactly that.
it lets you dynamically generate output. and some of that output could behtml, as we've typically been doing. and it's also, because it'sa programming language, is the mechanism via whichwe can talk to databases. >> and we can make queries toother servers like yahoos and programmatically do anythingreally that you might otherwise want to compel a computer to do. so php lets us startdynamically outputting content. so by this logic, i didn't havea dynamic website back in 1998.
>> it was just a static web page. my content had to be changed bymanually with gedit or some equivalent. but php is what we used orcould have used, rather, for something like thefrosh ims website, which was supposed to take registrations andmanage a list of users-- things that are actually changing overtime, even though we happen to use perl, a differentlanguage at the time. >> and then lastly, we introducedsql-- structured query language. so yet another languagethat's used for what?
used for what? can we venture slight--ok, we're not going to get much fartherthan the orchestra here. audience: it's a protocolused to talk to databases. david j. malan: a protocolused to talk to databases. let me tweak. it's an natural language usedto talk to databases-- selects and inserts and deletesand updates and actually even more features thatwe haven't even dived
into but you may want to explore-- haveto explore for, say, a final project. so there are these various pieces. >> and hopefully pset7, even thoughits specification is quite long, it's deliberately long to walk youthrough how these things can all be typed together. now, on monday, weintroduced our last language that we'll formally introduce inthe course-- that is, javascript. this, like php, is aninterpreted language. >> but a key distinctioni proposed on monday
is that whereas php is executing orbeing interpreted on the server, which in this case is the cs50 appliance,or might be some commercial web server on the internet,javascript generally is a language that runs client sidenot server side-- so in the browser. which is to say, just like when i openedup facebook source code and found all of those .js files, the implication wasthat when you visit facebook or most websites these days, you getnot only html, not only css, but a whole bunch of javascriptcode often in the form of .js files. and then it's the browser-- your ownmac or pc-- that executes that code.
>> but your browser executes it. you can think in sort of a sandbox. so that javascript code shouldn't beable to delete files on your computer. it shouldn't be able tosend emails on your behalf. your browser kind of restrictswhat you can do with it. >> so in that sense, it's a littleless powerful, perhaps, than c. but javascript can, as anaside, be used on the server, though we'll tend not to talkabout it in that context. so now let's tie these together.
a week plus ago, we presented some htmlon the left-- super boring web page. >> just says hello world. and then i proposed on theright we can kind of steal ideas from our discussion ofdata structures in c and think about how this hierarchicalmarkup language on the left could be drawn or implemented in memoryas an actual tree structure with nodes and pointers and those kinds of details. on the right, we callthat a dom-- document object model-- which is justa fancy way of saying tree.
>> now, why is this useful tothink of it in this way? because now withjavascript, because we have code that gets to play in thisenvironment, the actual html that's been sent to the browseralready and has already been loaded into memory by thebrowser into a tree in your computer's ram like this, we can use javascriptto actually traverse or walk or search or change that dom tree however we want. so in fact, if you thinkabout facebook.com, if you use the chat feature, if youuse gmail and the gchat feature,
anything where you havemessages coming again and again and again, those messages are probably,like, li tag, list item tags, maybe. >> or maybe they're justdivs that keep appearing every time you get an instant message. and so that just means whatfacebook or google is doing is any time you get amessage from the server, they are probably using javascriptto just add another node to this tree-- another node to thistree that then visually just looks like a new line of text on your screen.
but they're insertinginto this data structure. >> so in classes likecs124 and others, you'll actually write more code againstdata structures like this. but for now in javascript,we'll just assume we get all of this functionalityfor free from the language itself. so let's look at an example. >> let me open up a file called form.html. it's super simple. it just looks like this.
>> no css, no thought to aesthetics. it's purely functionaland apparently i'm asking for an email, a password,password again, and then a check to agree to some terms and conditions. what the source code for thislooks like is probably something you might guess with alittle bit of thought now. i've got a form tag here. >> an action is apparently going togo to a file called register.php. the method i'm going to use is get.
and then i've got a textfield whose name is email. >> i've got a password fieldwhose name is password. i've got anotherpassword field whose name is somewhat arbitrarily confirmation. it's just another http parameter. >> and then we we've not used these exceptsince the frosh ims demo in class-- a check box which isjust type equals check. and i'll call that agreement. so i've kind of arbitrarily butconveniently named these fields.
so that now when this form getssubmitted, let's see what happens. if i do malan@harvard.edu,i'll do a password of crimson. i'll do a password of nothing. let's not cooperate. >> and i won't check the box. let me click register. and it says, hm, you're registered. not really. >> but the url changed.
so this form was clearly allowedto submit to register.php. but presumably, i should becatching some of these errors. now, in pset7 and someof our lecture examples, we would generally print outa big red error message here saying, missing name,or missing password. we've done that before and we'vedone server side error detection. >> but many websites these daysdo client side error detection where the url doesn't change. the whole page doesn't refresh.
you get instant feedbackfrom the browser. maybe something goes red. >> maybe you get a pop up. but you don't waste time sending tothe server data that's incomplete. so let's see how we mightachieve that feature as well. >> let me go to form1.html,which looks the same. but if this time i domalan@harvard.edu and i type crimson and i don't cooperate furtherbut click register, notice now. it's not the sexiest solution.
i've at least caught this error. and i've used the alertfunction in javascript-- which we're only using in class. in general, you should not use thisbecause it can very quickly get out of control. but passwords do not match is the error. >> let me go ahead and click ok. but what the key takeaway hereis that the url did not change. so i've not bothered wastingthe server's time asking it
a question that i could havefigured out the answer to myself. >> and the user, even thoughbeen talking about this longer than the user'sgoing to think about this, is going to have instant feedback. there's no latency withthe network connectivity. so let's look at this source code. >> form1.html looksstructurally similar up here. the form is in fact the same. but let's see what i did down here.
and there's different ways to do this. and i've done the most straightfollower but not most elegant way yet. i have a script tag. i then calldocument.getelementbyid('registration'). and i store that valuein form, a variable. >> so what have i done? you can think ofdocument.getelementbyid as being a special function thatjavascript gives you that literally hands you apointer to one of the nodes
or rectangles in this tree. so now that's what our form variablein javascript is actually pointing at. >> so now the syntax is different fromc. but we're doing a few things here. one, this one is a little strangelooking, certainly versus c. but look at line 35. so on the left form.onsubmit. recall that onsubmit islike a field in a struct. if you think of the form variableis just being a c struct, it might have some fields.
>> back in the day, we had students names,ids, houses, those kind of fields. just think of onsubmit as another field. but it's a special field because thebrowser is preprogrammed to expect .onsubmit to not be a valuelike a number or string, but to actually be a functionor the address of a function in the computer's memory. >> and indeed, that's whatthis keyword here does. this says, give me a new function. but what is its namegoing to be, apparently?
>> thinking back to monday. what is the name of thisfunction based on this syntax? no, i mean, there's clearlyno name associated-- certainly not in what i've highlighted here. >> but that's actually ok. this is an anonymous function, or alambda function as some might call it. and that just meansit's still a function. it's just, you can't call it by name. but that's ok.
because again, the browser has beenpreprogrammed by companies like google or microsoft or mozilla or others tojust know that if the .onsubmit field inside of a form element has thevalue, treat it as a function-- a function pointer, if you will. and call it when the form is submitted. >> so what code should be executedwhen the form is submitted? apparently, everythinginside of the curly brace. and this is just stylistic. >> you could do this likewe tend to do in cs50.
but in javascript, most peopletend to keep it on the same line just because it more clearly isassociated with that keyword function. so now what am i doing? >> if form.email.value equals equalsthe empty string or nothing, here's an alert where i'm going to say,you must provide your email address, and then return false. and it's that return false thatprevents the form from being submitted. meanwhile, if the password value isblank, i'm going to yell at the user and say, you must provide a password.
>> meanwhile things are gettinga little fancier here. if form.password.value does notequal form.confirmation.value, the other field, yell atthe user that the passwords don't match as theydid not a moment ago. and then this one's alittle sexier because i know i knew conceptually thatchecked is a check box's name. >> so i can just use an exclamationpoint to say if the check is not checked-- it's the booleanvalue, true or false-- i'll yell at the user for that reason.
otherwise, if we get throughall of these conditions, let's just return true. let the form be submitted. and this will then happen. >> let's type in crimson. let's check the box, click register. and now i go through to the destination. now, there's no database there. there's nothing interestingin register.php.
i just needed somethingto actually talk to. so let me pause, here. any questions on what we've just doneor what some of this new syntax is? ok, yeah? >> audience: so any checkboxis automatically a boolean . you don't have to declare it like that. >> david j. malan: correct. any checkbox that's sent to you froman html form to your javascript code will be treated, yes, as aboolean value-- true or false.
it's a good question. whereas the other values, ofcourse, have been text, aka strings. >> all right, so let merewind a bit further. what was the whole point of this? just to be clear. like, we already know, even from pset7and even from last week's lecture examples, that we can obviously check$_get $_post see if the user give us an empty value. remember the empty function in php.
>> so just to be clear, what'sone reason we might also want to do this error checkinginside of the browser? what's the motivation here? >> audience: faster, and you don'tsend useless data to the server. david j. malan: good. it's faster. you don't send uselessdata to the server. >> so you get back a moreimmediate response. and overall, the userexperience is better.
think about the alternative. >> if for gmail-- and wasthe case many years ago. suppose you got a new email your gmailaccount, but the only way through to see that is to, like,reload the whole page. or suppose you click ona link to read an email. >> everything has to reload sothat you can see the email. or facebook-- you get a chat message. you don't see it until you reloadthe page or click some link. >> like, this would be an awfullyannoying user experience.
and this is what it was like,clearly, back when i ran for uc and the web was much less dynamicand javascript wasn't as popularized as it is now. and things are getting muchmore dynamic and much more client side in that sense. >> but there is a catch here, andthis is kind of an annoying gotcha. just because you add client sidedetection like this does not mean you can or should abandonserver side detection. you essentially want to put yourerror checking in both places.
because what was oneof the lesson learned from the article i read some excerptsfrom with this stupid cms system-- content management system-- that wasimplementing its authentication system, its login via what mechanism? javascript. >> audience: javascript. david j. malan:javascript, exactly, right? it was using javascript. and literally, you guys haveplayed a little bit probably
with chrome's inspector. and if i can find it, inspect element. >> let me go over to doall of chrome's options. and this is how easy it is todisable javascript in a browser. check, no more javascript. >> so in fairness, a lotof the web these days is just going to break becausegmail and other sites-- facebook-- assume thatjavascript is enabled. but if you're doing something stupidlike only validating users input
and checking it forerrors on the client side, an adversary could easily do this. and then even smarteradversary like you guys now could use telnet or curlor just command line commands and actually send messages to the serverthat similarly are not error checked. >> so this is more of auser interface decision than it is an actual technicalimprovement-- implementing something client side like this. so now a quick glance, but theni'll defer to the online walk
through for this one. in form two, we actually went throughand cleaned up the code a little bit. but let me defer to oneof the videos we'll likely embed in pset8 that just shows you asimilar syntax using a library called jquery, which is a super, superpopular library in javascript that frankly most peoplejust use these days and even confuse asbeing javascript itself. >> and it tends to involvesome dollar signs and keywords like documentin parentheses here.
but again, let me defer tosome slower tutorials online rather than get tied up in just syntax. let's move on tosomething a little cooler in terms of the applications of this. >> so in particular, let me goahead and open up this here. come on. >> let me open up this picture here. unnecessarily complicatedlooking, but it describes a technique called ajax--asynchronous javascript and xml, where
the x for xml is actuallyno longer really used. it tends to use somethingelse called json. >> but here is how something likegoogle maps or google earth works. let's try this on the fly, actually. let me go ahead and openup chrome on my browser. >> and let me go into,say, maps.google.com. and actually, if you're oldenough to remember what, like, mapquest was like back in the day,and maybe they still work like this. when you used to search for something--33 oxford street, cambridge, mass,
let's do this-- youwould actually, if you wanted to pan up anddown, left and right, you would look like abig arrow on top, and it would show you anotherframe of the map up here. or you would click left and youwould go over here, or another click and you would go over here. but instead thesedays, we of course just take for granted that we can goaround cambridge pretty quickly just by clicking and dragging.
but notice there's some glitches. >> if i do this fast enough,what seems to be happening as i drag a little too fastfor the computer to keep up? what do you see? >> audience: the pixels don't refresh. david j. malan: thepixels don't refresh. there's actually-- and youcould see this, actually, if you're watching online and pausethis or actually slows things down for once-- you'll see that there aretiles, squares, or rectangles that
are missing from the map untila split second later, more data, more images actuallyappear on the screen. and in fact, if we do this by lookingup chrome's-- let's say, chrome-- let's see. we can't do that. >> oh, whoops. let's open up maps.google.com. let me make the window bigger again. >> go back to 33 oxford street.
what was the website i was on recently? i had this, like, private rant tomyself that i'd then instant message any friend who was onlinewho wanted to hear it. there's some website. i think it's comcast-- soa very big american isp. you can, when signing up for new cablemodem service or cable tv service, they have a form very reasonablywhere they ask you for your address. and have this amazingfeature called auto complete, like google, that starts to fillin the answer to your question.
>> the problem is, they do auto completeon the first things that you type. so if you start typing in 33, itwill show you literally every house in america that startswith the number 33 before continuing toexpect you to type more. so if you type 33 oxford,then it shows you every street in america that has 33 oxford inits name, irrespective of the town that you're in. >> and then you continue typing. and finally, it realizes that they don'toffer service to your home in cambridge
or something like that. but the point is, this is the mostasinine implementation of auto complete ever. >> and i'm just going offon this tangent again. but there are good ways touse javascript and bad ways. and that's not necessarily the best one. >> but the point here, before thistirade, was to open up tools down here and open up developer tools,as we've encouraged before, and to watch the networktab as i click really fast.
and notice a whole bunchof get requests happened. all of this happened since i dragged. >> and most likely, indeeda lot of these rows now are image slash jpegmime types or content types. that's because what chrome is doingevery time i click and drag, click and drag, is it's realizing, oh, ineed to go ask google for the tile on the map that's over here,quickly download it via http, and then add it to the so-called domto the web browsers in memory tree representation so that the user,me, sees that updated tile.
and this is because ofa technique called ajax. back in the day, it reallywas the case that if you wanted to change what's on the screen,you would have to click up, down, left, right. and then a new page would open. but these days, everythingis more dynamic. it happens in the way we humans wouldhope it actually would interactively. and it achieves this byway of a technique called ajax, which is perhaps bestexplained by an example.
first, let me go aheadand open up a file called quote.php intoday's distribution code. >> and then let me do symbol-- whoops. let me do symbol=googfor just some stock. or actually, let's do theone from the pset free. enter. >> and now notice what i get back. so this is a reallyshort php file that i wrote that simply borrows codefrom pset7's lookup function
and spits out using this curly brace andquotes and colon notation, apparently, the current stock's price for thecompany that you pass in via get. so this is differentfrom most of what we've done in that notice i'mliterally spitting out what looks like javascript code. >> in fact, this is a javascript object. in fact, just to be more clear,javascript object notation-- json-- is just a fancy way of saying that youcan represent data in javascript much like you can in phpusing key value pairs.
so if i wanted to declarea variable in javascript to represents zamyla, forinstance-- a struct for zamyla-- and we'll call itstudent, this variable. her id is one, house iswinthrop, and name is zamyla. >> but i can also have an array of objects. so if i actually wanted to havean array in javascript containing multiple such objects, thistime representing staff, i might have these threechunks of code back to back to back for thesethree former staff members.
so the syntax, prettysimilar to both-- to php. but this is particularly javascript. it's object notation. so what is this useful for? >> if i write code that spits out json--javascript object notation-- stuff that looks like this or stuff thatlooks like zamyla's structure, i can actually use thisin programs i write. let me go to ajax0.html. and this too-- not muchthought given to aesthetics.
but watch what happens. >> let me go ahead and type free here. click get quote. and notice the url has not changed. but i did get a pop up with apparentlytoday's penny stock price of $0.15. so not all that bad. but the difference is that somehow,this data came back to me directly. but let's take a step towardsomething more familiar. in version one of this, let metype free again, click get quote,
and now-- oh, this wasactually the jquery version. so let me-- i didn'tfast-forward quite far enough. let me go to version two,which is where i wanted. notice what i've done here. i have a web page-- a supersimple version of any web page you might use today with a text fieldhere for free and then apparently just text. >> this is not a form here, apparently. but if i click getquote, notice my web page
is about to change as though ijust got a new instant message or as though i just moved themap and needed to get more data added dynamically to the web pagewithout the url changing and the user experience getting interrupted. indeed, i'm still at theexact same place-- ajax2.html. >> so let's look only at this exampleand see how this is happening. let me go into ajax2.html. and notice the form first. >> down here, i'm turningoff auto complete.
sometimes it getsannoying if the browser is trying to show youyour whole history. so you can do it in html byjust saying auto complete off. >> i've given this text field asymbol-- rather, an id of symbol. and now, this is an interesting feature. we haven't talked about span,but you can think about it like a paragraph tag or div tag. it's what's called anin-line element, which means you won't get a paragraphbreak above and below it.
it's just going to stay in-line withouthitting the equivalent of enter. so i have given this chunk of htmlto be determined a unique identifier that i arbitrarily called price. and i have a submit button. >> because now up here-- and this isactually super amazing how little code you can write to dorelatively neat things-- notice what i've done up here if iscroll up to the head of this page. i've included first inmy head a script tag that actually references ajavascript file elsewhere.
this is from the organizationthat writes jquery, and this is just giving you the latestversion of their jquery library. >> so this is kind of like sharpinclude in c or require in php. you use the script tagwith a source attribute. but now my own code isgoing to be right in here. >> notice i have a function called quotes. and it looks a littlecryptic at first glance. but let's tease this apart. give me a variable called url.
assign it literally this string. so, single quotes, double quotes injavascript just gives me a string. what does the plus do? concatenation. >> so this now is the jquery syntaxwhich takes a little getting used to. but this just means go get me the domnode whose unique identifier is symbol. the hashtag there meansunique identifier symbol. >> the dollar sign in theparentheses just mean, wrap this in jquery a sort of secret sauce soyou get additional functionality.
and then .val is apparentlya function, or as we say now, a method inside of this nodethat just gives you the value. so in short, ugly and confusingas this looks at first glance, this just means get with the user typedin, put it at the end of the string by concatenating it. that's all. >> so now, last three lines. you can squeeze a lot offunctionality out of three lines. this dollar sign, as anaside, is just a nickname
for a special global variablecalled literally jquery. >> dollar sign just looks cool. so the jquery community just kindof used it as their special symbol. it does not mean what it means in php. in javascript, dollar sign isjust like a letter of the alphabet or a number for a variable. >> you can just have it as the name. just looks cool. so the communityadopted it as a nickname
for their own library called jquery. >> and it's super popular. so get json is exactly that. it's a function that thefolks at jquery wrote that gets json from a server--javascript object notation. from what url is it goingto get that information? apparently from this url here. >> and what should the browser do assoon as it gets back this response? and this is the magic of ajax, so tospeak-- asynchronous javascript in xml.
it's hard to see with such asimple example as we had here. >> but this was asynchronous inthe sense that my code when executed sent a message to theserver to go get me some json. and it happened super fastthat i got a response. but what's interesting is that thisline of code didn't hang my computer. >> i didn't see a spinning icon. i didn't lose theability to move my mouse. my browser was actually perfectly fine. >> because the way javascript handles theresponse from the server is as follows.
you register what you'd calla callback function, which just means, hey, javascript. as soon as the serverresponds with json, please call this anonymous function. >> and please passed into this functionwhatever string the server spit out as an argument called data. so in other, words, ifi'm assembling dynamically a url quote.php passing in thissymbol like free or goog or whatnot, i'm then tellingjavascript go get that url.
remember that the browseris going to return something that looks like we saw earlier-- this. >> and what the second argumenthere to get json is saying is call this functionwhen the server gets back whether it's 10 millisecondsfrom now or 10 seconds from now. and as soon as you do,add the price to the page. this syntax here justmeans go get the node from the tree whose unique identifieris price-- that span we saw earlier. >> this method called htmljust says, go replace
the html that's there with data.price. what's data.price? well, the browser, recall,showed me this coming back. so this is data. >> and so it's a little crypticto see the commas here. but in fact, let me do this. let me just paste thisreal fast into gedit and show this like we showedzamyla's structure earlier. >> what the server is sending back is alittle object that looks like this.
and so data.price isjust giving me 0.1515. so a lot of movingparts here all at once. >> but the key takeaways isthat we have this ability to make additional httprequests using javascript without having to reload the page. and then we can actuallychange the web page on the fly. and it turns out thatjavascript and other languages can be used now not onlyto mutate web pages, but to actually write softwarein an actual computer,
not just confined to chrome or the like. >> in fact, if-- colton, would youlike to join us back up here with your lab code, and chang as well? let's go ahead, having talked aboutanonymous functions and callbacks and really tempt fate herewith a live demo with bleeding edge technology, one ofthese elite motion devices. now, this device, recall,is a little usb device as well that-- that's beautiful--that plugs in to your usb ports. >> and then it provides inputin the form of human gestures
by detecting using infrared beams,essentially, movements from your arm. so whereas what maria triedon before was muscular, actually feeling what's changingyour arm, this is infrared based. so it's looking for movements withinthe sort of sphere of a foot or so of the device itself. >> so why don't i takea stab at this first? and let's go ahead and throwyou up on the overhead here. so let's put colton's laptop up here. we've got andrew on the tv.
and what would you like me to do first? >> colton: go ahead and justput your hands over this guy and you'll see some fabulous glitter. >> david j. malan: very nice. this is all happening in real time. all right, and yep. so nice. all right, what else can we do? >> colton: go to the next screen and see.
>> david j. malan: all right. >> colton: a fun little gamewhere you get to make robots. >> david j. malan: all right, so thisis fake hands showing me what to do. colton: yes so go aheadand grab one of the blocks and put it on top of that robot's body. david j. malan: oh, there's my hand. oh. ok, adorable. wait a minute, ok.
>> colton: i made one on accident. >> david j. malan: ok, i'll get this guy. damn it! when we were practicing this lastnight, you know what this devolved into? >> like this. next one? >> colton: sure. >> david j. malan: all right,and there's a third. colton: and in this one, you get to--
david j. malan: oh,this one's beautiful. colton: --yeah, pick apart this flower. david j. malan: ok. no? missed. >> colton: oh, there you go. >> david j. malan: ah, look at that. very nice. well, why dont' we takeout one volunteer here
who would like to come on up. how about right therein the green, is it? >> all right, and let's have--instead of doing that, some of you might know this game here--cut the rope, perhaps? let's see. we have our glasses on over here? >> ok. thank you. what's your name?
>> audience: laura. >> david j. malan: laura? nice to see. if you don't mind puttinggoogle glass over your glasses. this is colton. >> colton: hi. nice to meet you. >> david j. malan: ok, come on around. all right, so what you're going todo here, having played this before,
is put your hand overthe leap motion here. and now your arrow should move. oh, nope. >> audience: no. >> david j. malan: wedon't want to quit yet. ok, wait. over here. so notice as you hold yourfinger over something, the mouse starts to go green,which is how you click.
>> so hover over play. and just one finger is fine. and now click on the littlegreen guy on the left. and now hold until it fills up green. now, like, level one up top. >> audience: yeah, we want level one, here. >> david j. malan: good. ok, so all you haveto do is cut the rope. your cursor's the white one down there.
all right, it's about to get harder. so hold your finger over next now. this one's hard. >> audience: oh crap. it wants to go that way. oh crap, that-- >> david j. malan: yeah. secondary goal is to get all the stars. all right, next.
>> let's see if you can get this third one. ok, go over there. >> sure. oh, very nice. >> so why don't we adjourn here today? let anyone come on up who wants to play. thanks so much to laura our volunteer. and we will see you on monday. >> audience: you probably want these back.
>> speaker 2: at the next cs50--