On the page there is a menu with buttons that, when clicked, should load content from php files and insert them into the corresponding divas. The page is static, without CMS. Tell me which of the 2 options described in the header is best suited for this? What is the difference between them?

    2 answers 2

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

    This is practically the description of the work of the function .load

    Therefore, in this case it is worth using it.

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

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

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

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

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

      If you don’t go into details, the only difference is that $(selector).load(url) loads the url data directly into $(selector) , and for $.get({...}) you need to write a handler.

      Use the first option if it satisfies you, but if you need to catch a load event, use $.get()

      UPDATE: In .load() it is also possible to connect the callback function when the download is successfully completed: .load( url [, data ] [, complete ] )

      Example:

       $( "#result" ).load( "ajax/test.html", function() { alert( "Load was performed." ); }); 
      • Yes, I need the corresponding function to be performed after successfully uploading the file. I understand that .load has no such possibility? - Cheg
      • @Cheg sorry, wrong, rarely .load , updated answer - Yevgeny Mironov