jquery – Choosing between .load and $.get

Question:

The page has a menu with buttons, when clicked, content from php files should be loaded and inserted into the corresponding divs. The page is static, without CMS. Tell me which of the 2 options described in the title is best suited for this? What is the difference between them?

Answer:

content from php files should be loaded and inserted into the appropriate divs

This is practically a description of how the .load function works.

Therefore, in this case, it is worth using it.

$.get only makes a GET request. At the same time, .load , depending on the parameters, can make a request of any type and even select a specific element from the result to be inserted into the page.

To do this, you need to specify the desired selector separated by a space in the url parameter.

 elements.load('site/url/ #element')

The above code will only insert the content of the element with id=element and not all the loaded markup.

The difference is also in the return value: $.get returns a Deferred (a Promise implementation), while .load returns the current collection, allowing you to continue working with it.

Scroll to Top