Javascript + jQuery: a crash course
Javascript
reference: http://learnxinyminutes.com/docs/javascript/
A function is declared with the function() keyword
function doSomething(thing) { return console.log(thing.toUpperCase()); } doSomething("lol");
Function objects don’t even have to be declared with a name – you can write an anonymous function definition directly into the arguments of another
setTimeout(function(){ // this code will be called in 5 seconds' time }, 5000);
jQuery event methods
reference: http://www.w3schools.com/jquery/jquery_events.asp
You must pass a function to the event
("p").click(function() { console.log("click!"); });
jQuery callback functions
reference: http://www.w3schools.com/jquery/jquery_callback.asp
A callback function is executed after the current effect is completed.
Typical syntax: $(selector).hide(speed,callback);
$("button").click(function(){ $("p").hide("slow",function(){ alert("The paragraph is now hidden"); }); });
jQuery and AJAX
reference: http://www.jquery-tutorial.net/ajax/the-get-and-post-methods/
Typical syntax: $.get/post(URL, params, callback);
$.post("test_post.php", { name: "John Doe", age: "42" }, function(data, textStatus) { alert("Response from server: " + data); });
The first parameter of the callback function is the content of the page requested. Since most of times this content is json stuff, I could access its fields with data.name and alike.
The second parameter to the callback function is the request exit status.
If I use get instead of post, it would perform a GET request like test_get.php?name=John Doe&age=42