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

Javascript App

Javascript App

Esteban Dorado Roldan

March 11, 2014
Tweet

More Decks by Esteban Dorado Roldan

Other Decks in Programming

Transcript

  1. About us Esteban Dorado @Mr_Esti • Computer Science • Web

    developer • Android developer Alberto J Gutiérrez @AJGutierrezJ • Degree Computer Science • Master Degree Software Development • Web Developer
  2. 1. What is JQuery? 2. Using JQuery 3. Searching/Traversing 4.

    Events/Animations 5. Styles 6. AJAX Agenda
  3. Jquery make it easy to: FIND TALK LISTEN CHANGE ANIMATE

    elements in an HTML doc over the network to fetch new content to what an user does and react accordingly HTML content content on the page
  4. Challenge: changing content <!DOCTYPE html> <html> <head> <title>Javascript App</title> </head>

    <body> <h1>Where do you want to go?</h1> <p>Plan your next challenge.</p> </body> </html> How can we modify the text of the <h1> element?
  5. Finding the proper HTML <!DOCTYPE html> <html> <head> <title>Javascript App</title>

    </head> <body> <h1>Where do you want to go?</h1> <p>Plan your next challenge.</p> </body> </html> How can we search through our HTML? • We need to understand how our browser organizes the HTML it receives.
  6. Document Object Model A tree-like structure created by browsers so

    we can quickly find HTML Elements using JavaScript. “DOM”
  7. What does that DOM structure look like? html head body

    title h1 p JavaScript App Where... Plan... • node types: element Text...
  8. How do find things using the DOM? Request Sends files

    Server Browser DOM Load Interacts
  9. <!DOCTYPE html> <html> <head> <title>Javascript App</title> </head> <body> <h1>Where do

    you want to go?</h1> <p>Plan your next challenge.</p> </body> </html> Using the JQuery function to find nodes JQuery(”h1”); JQuery(”p”); $(”h1”); $(”p”); JQuery selectors short & sweet syntax
  10. Example 1 How can we modify the text of the

    <h1> element? HTML document <!DOCTYPE html> <html> <head> <title>Javascript App</title> </head> <body> <h1>Where do you want to go?</h1> <p>Plan your next challenge.</p> </body> </html>
  11. Our completed code jQuery(document).ready(function(){ $(“h1”).text(“Where are you?”); }); Note: We

    need to make sure the DOM has finished loading the HTML content before we can reliably use jQuery.
  12. JQuery 1.x vs JQuery 2.x • JQuery 2.x has the

    same API as JQuery 1.x • JQuery 2.x does not support IE 6, 7, or 8
  13. Getting started 1. Download jquery 2. Load it in your

    HTML document 3. Start using it <script src=”jquery.min.js”></script> <script src=”code.js”></script>
  14. We can find elements by ID or Class nav {

    … } .card_color { … } #container { … } $(“nav”); $(“.card_color”); $(“#container”); CSS JQuery
  15. Selecting descendants <h1>Where do you want to go?</h1> <p>Plan your

    next adventure.</p> <ul id=”countries”> <li class=”promo”>Spain</li> <li>UK</li> <li>USA</li> </ul> How do we find the <li> elements? $(”countries li”); parent descendant
  16. Exercise 2 Select <li> element. <h1>Where do you want to

    go?</h1> <p>Plan your next adventure.</p> <ul id="countries"> <li>Spain</li> <li> <ul id="uk"> <li>London</li> </ul> </li> <li>USA</li> </ul>
  17. Exercise 3 Select <li> element. <h1>Where do you want to

    go?</h1> <p>Plan your next adventure.</p> <ul id="countries"> <li>Spain</li> <li> <ul id="uk"> <li>London</li> </ul> </li> <li>USA</li> </ul>
  18. Exercise 4 Modifying the text of the <li> element. <h1>Where

    do you want to go?</h1> <p>Plan your next adventure.</p> <ul id="countries"> <li>Spain</li> <li>UK</li> <li>USA</li> </ul>
  19. Exercise 5 Modifying the text of the <li> element, but

    even elements. <h1>Where do you want to go?</h1> <p>Plan your next adventure.</p> <ul id="countries"> <li>Spain</li> <li>UK</li> <li>USA</li> <li>Germany</li> </ul>
  20. Exercise 6 Select <li> element. <h1>Where do you want to

    go?</h1> <p>Plan your next adventure.</p> <ul id="countries"> <li>Spain</li> <li>UK</li> <li>USA</li> </ul>
  21. Changing our Style $(document).ready(function(){ $(“#maps”).on(“click”, “.countries”,function() { $(this).css("background-color","#333"); $(this).css("border-color","1px solid

    #123"); }); }); $(this).css("background-color","#333") .css("border-color","1px solid #123"); $(this).css("background-color":"#333", "border-color":"1px solid #123");
  22. Moving Styles to Externall CSS $(document).ready(function(){ $(“#maps”).on(“click”, “.countries”,function() { $(this).addClass("showData");

    }); }); .showData{ background-color:"#333"; border-color:"1px solid #123"; } .showData .price{ display: block; }
  23. Ajax Ajax: Asynchronous JavaScript And XML Anyone called httpRequest. Webapp

    interactive or RIA (Rich Internet Applications) Examples: Gmail, Google Maps, Youtube, Dropbox...
  24. Other example var datas = null; $.ajax({ url: ‘ajax/test.html’, success:

    function(data) { datas = data; } }); console.log(datas);
  25. Other example I var datas = null; $.ajax({ url: ‘ajax/test.html’,

    async: false, success: function(data) { datas = data; } }); console.log(datas);
  26. Other example II var datas = null; $.ajax({ url: ‘ajax/test.html’,

    success: function(data) { datas = data; } }).done(function(msg){ console.log(datas); });
  27. Other example III var datas = null; $.ajax({ url: ‘ajax/test.html’,

    success: function(data) { datas = data; }, error: function(){ datas = { v1: ’info’ }; } }).done(function(msg){ console.log(datas); });
  28. Others methods load $.load(URL, data, callback); $.get(URL, callback); $.post(URL, data,

    callback); get post getScript getJSON $.getScript(URL, data, callback); $.getJSON(URL, data, callback);
  29. Q&A