Here is a quite straight forward jQuery code I find on one site:
//It simply loads more news
function ReadMore(page){
$("#news-list").html('Loading More News...');
$("#news-list").append('<img src="/images/loading.gif">');
next=parseInt(page)+1;
html=$.get('/morenews.php','page='+page,function (data){
$("#News").append(data);
$("#news-list").html("<a href='javascript:void(0);' onclick='ReadMore("+next+");'>More News</a>");
});
}
My question is:
-
What kind of datatype is it using? “Expected data type of the response. One of: null, ‘xml’, ‘script’, or ‘json’. ”
-
What is
html=$.get('/newread.php','page='+page,function (data){}in jQuery? I have never seen$.getand there seems to be one parameterpage. -
Does the server receive something like this: /morenews.php?page=3
-
This code cant deal with errors. If the server is too busy, let’s say, doing nothing for 20 seconds, how can I add some error messages?
By the way, jQuery seems to be a weird language because it constantly create anonymous functions.
pageis an integer (although it gets typecast to a string when sent to the server as part of an HTTP GET request).datais an html string.See
jQuery.get( url , data , success() ). It’s shorthand for $.ajax(). Thesuccess()function is a callback, which gets exectuted once a response is received from the server.Yes: /morenews.php?page=3
You could try adding a timeout function to cancel the request if there’s been no response within a given time.
The language is Javascript (jQuery is just a library), and anonymous functions are really cool when you get used to them…