My jQuery function is making a ajax call (GET request) and it has to return HTML that I will then inject in the page in the success event.
What do I have to escape in the HTML to ensure there aren't any issues?
My jQuery function is making a ajax call (GET request) and it has to return HTML that I will then inject in the page in the success event.
What do I have to escape in the HTML to ensure there aren't any issues?
Look up jQuery's load method, this will do everything you want without the extra parsing.
From Visual jQuery (www.visualjquery.com):
In jQuery 1.2 you can now specify a jQuery selector in the URL. Doing so will filter the incoming HTML document, only injecting the elements that match the selector. The syntax looks something like "url #some > selector". See the examples for more information.
Returns: jQuery
Parameters:
url (String): The URL of the HTML page to load. data (Map): Key/value pairs that will be sent to the server. callback (Callback): The function called when the ajax request is complete (not necessarily success). function (responseText, textStatus, XMLHttpRequest) {
this; // dom element } ExampleLoad a piece of the documentation sidebar navigation into a custom unordered list.
jQuery Code
$("#links").load("/Main_Page #p-Getting-Started li");
The example given will load the url /Main_Page, extracting the contents inside each LI inside the ID p-Getting-Started, and insert them directly into the ID 'links' in your page.
I use this technique in conjunction with Modernizr to load up product pages in a popup "window" inside my web app if the browser is not a mobile browser. Less than 20 lines of JS/jQuery code makes the web site more interactive for its desktop users. Be sure to explore the options the load() method has, you can limit results and it also has a success callback.