Javascript – Refresh part of a page using JQuery

htmljavascriptjquery

Is this a valid way to refresh part of webpage:

$("#content").load(location.href+" #content>*","");

Note that I'm not requesting any new data here, but basically re-loading the content of a div as part of an .ajax success function.

This seems much easier than requesting data through an ajax function and loading that into the page, but I'm wondering if there are any drawbacks or issues with this method.

Best Answer

This seems much easier than requesting data through an ajax function and loading that into the page,

The .load() method sends an AJAX request, so no, it's not easier, it's absolutely the same :-) In addition to the standard $.get(), $.post() and $.ajax() methods it allows you to provide a selector so that you can fetch only some portion of the returned HTML during the AJAX request. Maybe it is this that makes it more convenient in some situations. But behind the scenes all of those method end up calling $.ajax(). They are just shorthands.