JQuery Ajax loading div question

I have a somewhat thoughtful question regarding jQuery Ajax. My question is this: if I load only one HTML section into a div using jQuery Ajax, for example:

$('#result').load('ajax/test.html #container'); 

The user must download the entire test.html file to find out what the #container div is? In other words, will the entire test.html file be downloaded and then parsed only to select the div #container, or will only the #container div file be downloaded? If the first happens, can you come up with some way to just load the div #container without creating a new html file?

Thank you so much for your contribution! :) I really appreciate that.

+4
source share
3 answers

Of course, all the HTML will be loaded.

If you want to avoid this, you need to create some kind of serveride script, for example PHP, which will send only the necessary content.

You could call it something like

 $('#result').load('ajax/test.php?ajax=1'); 

and in PHP check for the existence of $_GET['ajax'] , and if it exists, send only the #container div.

+3
source

See: http://api.jquery.com/load/

$ ('# result'). load ('ajax / test.html #container');

When this method is executed, it extracts the contents of ajax / test.html , but then jQuery parses the returned document to find the element with the container identifier . This element, together with its contents, is inserted into the element with the result identifier,, and the rest of the extracted document is discarded .

+3
source

The whole page will be loaded. After jQuery has content that it parses to find the appropriate choice. See Documents for downloading jquery

When this method is executed, it extracts the contents of ajax / test.html, but then jQuery parses the returned document to find the element with the container identifier. This element, together with its contents, is inserted into the element with the result identifier, and the rest of the extracted document is discarded.

+1
source

Source: https://habr.com/ru/post/1345572/


All Articles