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

Front-end Workshop

Daniel Shearmur
April 22, 2013
130

Front-end Workshop

Daniel Shearmur

April 22, 2013
Tweet

Transcript

  1. How we strive to write JavaScript • Script tags at

    the end of the page • Majority of code in external files • Only a small amount of global variables • Progressive enhancement
  2. jQuery or $ Can take most things as a first

    argument: • "<h1>Some title</h1>" • "#some .selector" • An element • functions Returns a jQuery set • Array-like object (normally dom elements) • Has all the jQuery methods available
  3. Chaining $('#some-element').addClass('is-cool') .append('<span class="icon-tool" />') .css({ color: 'black', fontSize: '20px'

    }); Get: $(el).thing('name') Set: $(el).thing('name', 'value') Getters and setters
  4. When to execute? <body onload="begin()"> window.onload = function () {}

    $(document).ready(function () {/* do things */}); $(function () { /* shorthand */ });
  5. Styles Set some CSS: $(el).css('property', 'value') Get some CSS value:

    $(el).css('property') Set more CSS: $(el).css({ 'property1': 'value1', 'property2': 'value2' });
  6. AJAX var promise = $.ajax({ type: 'post', url: '/some-path', data:

    { dan: 'is cool' } }); promise.success(function (data) { /* do something */ }); promise.error(function (error) { /* report error */ });
  7. JSONP <script> function cb(data) { // do something with data

    } </script> <script src="http://some-api/?callback=cb"></script>