Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How the Web Works: Lecture 6

How the Web Works: Lecture 6

This talk was designed for a class (98-135) taught at Carnegie Mellon University in Spring 2010.

Abhinav Sharma

January 09, 2014
Tweet

More Decks by Abhinav Sharma

Other Decks in Education

Transcript

  1. Homework 1 Kept Changing, finally settled Out Before Spring Break

    Covers PHP, MySQL, HTML, CSS, MVC We’ll talk about MVC after the break
  2. Midsemester Grade Since only HW is compulsory If you’ve done

    a mini, Pass! Otherwise... Quick Online Quiz Email Me
  3. Upcoming Mini 5: Javascript Mini 6: MySQL Mini 7: Firebug

    HW1: PHP MySQL HTML CSS HW2: Javascript, AJAX, jQuery
  4. But Why? Called Mocha, LiveScript Browser Wars Netscape vs. IE

    Java was Hot Coincided with Java Support Publicity Stunt
  5. <script> function changecolor() { // changes bg color } </script>

    <body> <a href=”#” onclick=”changecolor()”>
  6. <script> function changecolor() { // changes bg color } </script>

    <body> <a href=”#” onclick=”changecolor()”> Click Me
  7. <script> function changecolor() { // changes bg color } </script>

    <body> <a href=”#” onclick=”changecolor()”> Click Me </a>
  8. <script> function changecolor() { // changes bg color } </script>

    <body> <a href=”#” onclick=”changecolor()”> Click Me </a> </body>
  9. AJAX Just a simple, powerful idea Asynchronous Javascript & XML

    Uses XMLHttpRequest() HTTP Request only new data Use JS to replace the HTML Told you HTTP is important
  10. DOM Tree How the Browser Sees HTML Makes Manipulation Easy

    JS: Manipulate Nodes (Elements) Traversal Not Required Direct Access with IDs
  11. JS Basics Runs inside a web page Plan of Action:

    - Write Hello World - Learn in Firebug - Write useful real code
  12. The Big Picture JS involves GUI Programming We define functions

    Listen for events (ex. Click) Call functions on event Listener: Code that waits Handler: Code that calls
  13. Firebug Eclipse : Java :: Firebug : JS JS must

    run in the browser ... so Firebug is a Firefox plugin Powerful Debugger Handy for Development too... Demo Time! http://www.cmurpg.com/assignment.php?n=6&v=b5
  14. Syntax Overview End lines with ; Case Sensitive Interpreted Syntax

    similar to Java Variables Weakly Typed! The above are from PHP!
  15. Variables, Operators Declared with var No explicit type! Assign with

    = operator No integer / floats Good ‘ol operators Boolean true or false
  16. Arrays NOT HashTables Created as var arr = [] Add

    with push() C/Java Indexing: arr[i] arr.length slice(), concat(), join() http://www.cmurpg.com/assignment.php?n=6&v=b10
  17. Control Flow Very Similar to Java if, else if switch

    while do/while for break continue try/catch
  18. Functions Keyword function No return types Take/not take arguments Overloading

    Very similar to PHP function count($i, $max = 10) { while ($i < $max) { print $i; } } function factorial($n) { if ($n == 0) return 0; return $n * factorial($n-1); }
  19. Functions Keyword function No return types Take/not take arguments Overloading

    Very similar to PHP function count(i, max = 10) { while (i < max) { console.log(i); } } function factorial(n) { if (n == 0) return 0; return n * factorial(n-1); }
  20. Classes Do Not Exist! Such an $#%#$% mess! But we

    love them... So we’ll mimick them Using two other things! class Dog { string name; int id; void bark(); }
  21. Classes Do Not Exist! Such an $#%#$% mess! But we

    love them... So we’ll mimick them Using two other things! class Dog { string name; int id; void bark(); }
  22. Hash Tables Different from Arrays Very Important because.. JS Doesn’t

    have classes These replace objects! Higher-Order functions! foo bar baz gorp
  23. First Class Functions Its a lot, for now think... Functions

    are like values! Can be passed around! f()
  24. function Cook( i1, i2, f ) { alert("get the "

    + i1); f(i1); f(i2); } Cook( "lobster", "water", SeafoodCooker ); Cook( "beef", "seasoning", Grille );
  25. This is Crazy Ok, but how do I mimick a

    Class, not just objects?
  26. f()

  27. Selectors Many Different Ways By ID, Name, etc. (like CSS)

    document.getElementByID(id) Lets see it in action!
  28. The General Idea Select Element from DOM Tree By ID,

    Class, etc.. (ID in this case) Manipulate the Node (Element) ex. Change HTML inside it!
  29. Listeners Listener: - Node + Action + Function If that

    action happens... ... the listener calls the function elem.addEventListener(...) More Demos!
  30. Using XHR xmlhttp = new XMLHttpRequest(); Create Object xmlhttp.open(“GET”, url,

    false) Open Request Object xmlhttp.send(null) Make Request elem.innerHTML = xmlhttp.responseText Change Page
  31. Using XHR xmlhttp = new XMLHttpRequest(); Create Object xmlhttp.open(“GET”, url,

    false) Open Request Object xmlhttp.send(null) Make Request elem.innerHTML = xmlhttp.responseText Change Page
  32. Using XHR xmlhttp = new XMLHttpRequest(); Create Object xmlhttp.open(“GET”, url,

    false) Open Request Object xmlhttp.send(null) Make Request elem.innerHTML = xmlhttp.responseText Change Page
  33. Using XHR xmlhttp = new XMLHttpRequest(); Create Object xmlhttp.open(“GET”, url,

    false) Open Request Object xmlhttp.send(null) Make Request elem.innerHTML = xmlhttp.responseText Change Page
  34. Using XHR xmlhttp = new XMLHttpRequest(); Create Object xmlhttp.open(“GET”, url,

    false) Open Request Object xmlhttp.send(null) Make Request elem.innerHTML = xmlhttp.responseText Change Page Fails on IE6 ActiveXObject instead of XMLHttpRequest
  35. Making it Useful Send Params with GET ... to a

    PHP URL Returns smart result Put it on the page Now we’re talking!
  36. AJAXML XML: Format of fetched data But we used plain

    text! Two Choices: XML or JSON Extended Markup Language Javascript Object Notation
  37. Why? Because HTTP can return strings So PHP returns a

    JSON string JS converts back to object Can do with XML too JSON is the easiest.
  38. jQuery So what is a framework? Common patterns in Code

    Consistency across browsers Common actions quicker
  39. jQuery Style $(selector).action(handler_function); Like CSS selectors: #my_id .my_class elem Defined

    in API keypress, click, blur, lots more... Something to be done... function blow_up_world() Special jQuery Alias
  40. jQuery AJAX GET $.get(target, data, handler); Script to send data

    to, ex. check.php Data to send, as a hash table
  41. jQuery AJAX GET $.get(target, data, handler); Script to send data

    to, ex. check.php Data to send, as a hash table Function to handle response
  42. jQuery AJAX GET $.get(target, data, handler); Script to send data

    to, ex. check.php Data to send, as a hash table Function to handle response Cross-Browser Compatible | Detects IE
  43. JS Outline If button clicked, called checker Get username from

    form Make GET request, handle Response Handler
  44. After the Break What I really wanted to teach Now,

    you have the tools Lets put them together! ... and make awesome stuff!
  45. Webmasters’ World Putting it together Domains, Web Hosting Publishing on

    the Web CMS, Frameworks Analytics, SEO, Ads DNS, Caching, other stuff...
  46. Building Social Apps Connect to Social Sites Facebook, Twitter, YouTube

    REST APIs Socially Rich Applications HW4 implements Connect
  47. Photo Credits http://api.ning.com/files/KXomTHEv7nIaor6MGM4xe*z6tf2LyIygHOOt8PQr432b0CwDaCD8tHA70HEI-liDXvxAu3maRWLYQyk9o-A0-sTbHILeR6Qn/JavaLogo_1.jpg http://2.bp.blogspot.com/_YYZ0pJ9j30U/SSpfQEZ9u9I/AAAAAAAAAyY/Crdymakw7xg/s320/Internet_Explorer_7_Logo.png http://fullygeek.com/wp-content/uploads/2007/12/netscape9logo.png http://getfirebug.com/img/firebug-large.png http://farm3.static.flickr.com/2326/2378867408_4cc90791d6.jpg http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif http://www.cmurpg.com/assignments/a6/img/b23.png http://www.meredithlibrary.org/images/homeworkhelp.jpg

    http://www.flickr.com/photos/mcquinn/2302027163/ http://www.coolestspringbreak.com/image-files/spring-break-crowd.jpg http://www.holidaysparadise.com/wp-content/uploads/2009/02/cancun-beach-spring-party.jpg http://www.flickr.com/photos/wirenine/395678424/ http://www.flickr.com/photos/maydayrelay/3436387683/sizes/l/ http://www.flickr.com/photos/68346513@N00/521465221/ http://www.andreaharner.com/archives/xmen-3.jpg http://www.flickr.com/photos/richardmoross/3132598166/ http://www.wonderday.ca/blog/wp-content/uploads/2009/05/x_men_origins_wolverine_movie_poster1.jpg