JQUERY, PART 2

5 downloads 1538 Views 80KB Size Report
AJAX with jQuery. Simplest method of ... JavaScript Object Notation (JSON). Provides a mechanism for storing ... Reconstituting an object from a JSON string:.
11/16/2009

JQUERY, PART 2

Fall 2009

CSCI 3110  Advanced Topics in Web Development

Retrieving and Modifying element content $(sel).html() Returns the content of the first element that matches  the selector (Same as innerHTML) the selector. (Same as innerHTML)

$(sel).html(text) Sets the content of all the elements matching the  selector. http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐1.htm

1

11/16/2009

Retrieving and Modifying element content $(sel).text() Returns the content of all the elements that match  the selector as a single string the selector as a single string

$(sel).text(newText) Sets the content of all the elements matching the  selector.   If the contents of newText contains  they are  translated to HTML entities http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐2.htm

Removing and Emptying elements $(sel).remove() Removes all elements matching the selector from the  document. document http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐3.htm

$(sel).empty() Empties the contents of all elements matching the  selector. h // i http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐4.htm i d / i /CSCI3110/ l /12 4 h

2

11/16/2009

Reading values within forms $(sel).val() Reads value from first element that matches the  selector. selector Must be careful with checkboxes or radio buttons as  returns the first value regardless of whether  checked. http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐5.htm

SSolve the radio button or checkbox problem with the  l th di b tt h kb bl ith th selector :checked http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐6.htm

Setting values within forms $(sel).val(text) Sets value of all elements that matches the selector. http://einstein etsu edu/~pittares/CSCI3110/examples/12a 7 htm http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐7.htm

3

11/16/2009

AJAX with jQuery Simplest method of asynchronous external content  loading, directly into page content:   $( l) l d(URL [ $(sel).load(URL, [parameters], [callback function]) t ] [ llb k f ti ]) Parameters‐‐if a string GET method is used. If a map of  key/value pairs, POST is used.   Callback function‐‐runs after the load is accomplished  (provides diagnostic response, etc.) http://einstein etsu edu/~pittares/CSCI3110/examples/12a 8 htm http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐8.htm

AJAX with jQuery Alternate method of external content retrieval,  loading or other processing under our control  (function returns the XHLHttpRequest) (function returns the XHLHttpRequest): $.get(url, [data], [callback function], [response type]) Data‐‐set of key/value pairs in map format.  Always  sent as GET string. Callback function‐‐takes response and processes Type‐‐"xml", "html", "script", "json", "jsonp", "text" " l" "h l" " i " "j " "j " " " http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐9.htm

4

11/16/2009

AJAX with jQuery Alternate method of external content retrieval,  loading or other processing under our control  (function returns the XHLHttpRequest) (function returns the XHLHttpRequest): $.post(url, [data], [callback function], [response  type]) Data‐‐set of key/value pairs in map format.  Always  sent as POST packet. C llb k f Callback function‐‐takes response and processes i k d Type‐‐"xml", "html", "script", "json", "jsonp", "text"

JavaScript Object Notation (JSON) Provides a mechanism for storing and retrieving  (serializing) object data. htt // http://www.json.org/ j /

Useful with AJAX. Rules: Object members contained within { }. Members consist of comma separated field:value p pairs. Arrays are contained within [ ].

5

11/16/2009

Example JSON object representation { "petType": "cat", "age": "7", "gender": "male", "name": "Marble", "favoriteToys": ["ball","string","fake mouse"] }

Function library json2.js can be downloaded from  http://www.json.org/json2.js

Working with JSON Reconstituting an object from a JSON string: JSON.parse(JSONstring);    R t Returns an object bj t http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐10.htm

Converting and object to a JSON string JSON.stringify(object); Returns a JSON string h // http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐11.htm d / / / l / h

6

11/16/2009

Working with JSON in jQuery Retrieving a JSON object via AJAX request $.getJSON(url, [data], [callback function]) Data‐‐ D t if a string, GET method is used. If a map of  if t i GET th d i d If f key/value pairs, POST is used.  Callback function‐‐takes response and processes http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐12.htm http://docs.jquery.com/Utilities/jQuery.each#objectcallback http://einstein.etsu.edu/ pittares/CSCI3110/examples/12a 13.htm http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐13.htm http://einstein.etsu.edu/~pittares/CSCI3110/examples/12a‐14.htm

Ajax with the most control $.ajax(options) takes in an object that consists of  key/value pairs and returns the XHLHttpRequest object. object Allows specifying payload, type of response, target  URL, timeout values, functions on success, functions  on error, functions done before sending, etc. http://docs.jquery.com/Ajax/jQuery.ajax#options

7