Refresh part of a page using JQuery
Is this a valid way to refresh part of webpage:
$("#content").load(开发者_StackOverflow中文版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.
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.
If you want to refresh part of a web page, say the content inside a element which has an ID or class of "content" : <div id="content" class="content">Content to refresh via ajax...</div>
$('#content').load('ajax/newcontent.html');
or
$('.content').load('ajax/newcontent.html');
The content returned from the url "ajax/newcontent.html" will replace the content inside the div element with an id or class = "content".
On this page: http://blog.mediasoft.be/partial-page-refresh-with-ajax-and-jquery/ it appears that some people are having trouble getting this to work on IE, so I'm going to assume it's not 100% reliable yet and will continue to use the standard way of refreshing content using JQuery for now.
Actually there are no issues, the load
action is a wrapper for jQuery.ajax
it makes also some validations and assigns the html to the selected element.
You could reload a section of the visible page using iframes, if you can accept their limitations.
Thanks @aruss. Just to be clear I'm not returning any HTML during the load action, but rather loading HTML from a div that's in the current webpage. – rolling stone 11 mins ago
--
Then there is no need to use the load() method. It's as simple as
$('#content').empty().html($('#idOfContentNeeded').html());
精彩评论