Javascript, HTML, SVG, and generic web development techniques Entry: jQuery Date: Mon Mar 15 17:16:27 CET 2010 As a first dive into the world of javascript and jquery, I'm reading [1]. First conclusion: callbacks and proper lambdas ;) Question: what is the significance of the '$' character? Is it just a function? It's the jQuery constructor, an alias for the jQuery method. Moving on to [2]. Passing by [3] to find out about the $ sign. That page also mentions Note that CSS stylesheets come before the jQuery file, and the jQuery file comes before your custom script. So it seems jQuery is mostly about iterating over filtered parts of Moving on to [2] the document, i.e. document queries. Makes sense. Question: what does the `this' object point to in an anonymous function? Maybe the object it is pasted into? [1] http://docs.jquery.com/Tutorials:How_jQuery_Works [2] http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery [3] http://www.learningjquery.com/2006/09/introducing-document-ready [4] http://www.quirksmode.org/js/this.html Entry: jQuery.each() Date: Thu Mar 18 15:12:06 CET 2010 From [1]: A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties. [1] http://api.jquery.com/jQuery.each/ Entry: Rhino (just JavaScript) Date: Thu Mar 18 15:33:58 CET 2010 java -classpath ~/sw/bin/js.jar org.mozilla.javascript.tools.shell.Main // lambda: abstraction and application js> (function(a){return 1+a;})(1) 2 // objects js> function Test() { this.foo = 123; } js> new Test() [object Object] js> (new Test()).constructor js> (new Test()).bar js> (new Test()).foo 123 js> (new Test()).bar == false false js> (new Test()).bar == undefined true // It seems that capitalized constructors is just a convention. js> function foo() { this.bar = 123; } js> new foo() [object Object] // The global object (namespace). js> this [object global] js> (function() {return this;})() [object global] // Each object is a separate namespace. js> function foo() { this.bar = 123; this.foo = function() {return this;}} js> (new foo()).foo() [object Object] // Prototypes: add a .prototype object in the constructor js> foo.prototype [object Object] js> foo.prototype.abc = 1 1 js> (new foo()).abc 1 // Reflection: list properties. Iteration over an object (associative // array) returns a collection of strings representing slot names. js> for (att in new foo()) { print (att); } bar foo abc // I.e. cfr arrays: js> for (att in ["1","2","3"]) { print (att); } 0 1 2 // Data definition: JSON js> var a = {"foo" : 123, "bar" : 345} // Object (associative array) indexing js> for (it in a) { print(it,a[it]); } foo 123 bar 345 // Quotes are optional js> var a = {foo : 123, bar : 345} // Applying functions (methods) to objects. Can use call or apply. js> obj = new Object() [object Object] js> obj.x = 123 123 js> send = function(obj,msg) { return msg.call(obj); } function (obj, msg) { return msg.call(obj); } js> send(obj, function() {return this.x;}) 123 js> send2 = function(obj,msg) { return msg.apply(obj,[]); } function (obj, msg) { return msg.apply(obj, []); } js> send2(obj, function() {return this.x;}) 123 Entry: The DOM Date: Thu Mar 18 17:27:38 CET 2010 In the DOM, the root object is the window. The window.document represents the currently viewed document. How do you get to the clients of a node? In XML there are two sub-containers of a node: attributes and child elements. The lowest level representation in the DOM chains these together using the following properties: .attributes an array of NamedNodeMap objects .childNodes an array of nodes Attributes can also be accessed using the .getAttribute() method. Also see [1]. In general it is simpler to use iterator and query functions to access parts of the DOM. [1] http://www.howtocreate.co.uk/tutorials/javascript/dombasics Entry: jQuery Date: Fri Mar 19 10:17:47 CET 2010 The following is in the Firebug console. The prototypical use of jQuery is to iterate over a collection. >>> $("a").map(function(indx, el) {return this.getAttribute("href");}) ["http://www.sipri.org/"] The $ here is an alias for the jQuery constructor. $("a") constructs a query which abstracts a collection of all nodes in the document. Query objects supports the `each' method which applies each object in the query to a function. Also supported is the `map' method which additionally collects function results in a list. That particular example is simpler to do as: >>> $("a").attr("href") "http://www.sipri.org/" which returns the attribute for the first element in the selection. Most of the API seems to be imperative; methods modify the state of the document. To set the text of a collection of nodes, use: >>> $("a").text("SIPRI") [a www.sipri.org] The syntax that's used in the string passed to to the $() constructor has elements of XPath. [1] http://en.wikipedia.org/wiki/Jquery Entry: Crash course JavaScript + DOM + jQuery Date: Fri Mar 19 11:04:26 CET 2010 1. Play with JavaScript independently from the DOM on the console using Rhino. Download Rhino from here[1]. Unpack + compile. The product is the "js.jar" file. Invoke using java -classpath ~/sw/bin/js.jar org.mozilla.javascript.tools.shell.Main Make sure you understand: OBJECT: An object is an associative array (maps string -> object) FIELD: Object fields do not need to be declared. A field fld in object obj can be retrieved as: obj.fld obj["fld"] Both styles can be used to read _and_ assign values. FUNCTIONS: functions are objects: they can be assigned to fields and passed as arguments to functions CONSTRUCTORS: functions serve as constructors: new myFun() assigns a new object to the "this" object used in the function. PROTOTYPES: what I said above is wrong. Objects are initialized from a ``prototype object''. The default prototype object is the empty object. new myFun() assigns a copy of the myFun.prototype object to the "this" object used in the function. THIS: Refers to the current object = the object the current function was invoked on. If a function is not invoked on an object, the "this" object is the global object (containing global variables). In the DOM the global object is window. JSON: The JSON syntax is the syntax for initializing JavaScript objects. (JavaScript Object Notation). 2. Get to know the Document Object Model (DOM) a bit using Firebug[2]. The DOM is a representation of everything there is to know about a web page and is context, represented as a hierarchy of JavaScript objects and functions (i.e. iterators). A very useful part in Firebug is the JavaScript console. This is essentially the same as Rhino above, but with extra functionality (objects & functions). Learn that the DOM is a mess, and that abstractions on top of it are necessary. But, as with all abstractions, it is useful to know the low-level substrate on which they are built. 3. jQuery[3] essentially provides a way to "walk the DOM", i.e. to query structures inside the DOM and iterate over them. [1] http://www.mozilla.org/rhino/download.html [2] http://www.joehewitt.com/software/firebug/faq.php [3] http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery Entry: Ajax Date: Thu Mar 25 13:28:03 CET 2010 In order to make Ajax work in firefox, all the files need to come from the same domain. Apparently this doesn't work with file:/// urls. A quick workaround is to set up a web server using: cd