jQuery ajax load not a function

54.8k views Asked by At

This example

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<div id="test"></div>
<script>
$(document).ready(() => {
  $('#test').load('doesntmatter');
});
</script>

seemed to me like it is identical to the examples for the ajax load function. As the code snippet can tell you, it actually errors out with

Uncaught TypeError: $(...).load is not a function

What am I doing wrong?

9

There are 9 answers

0
Guillaume CR On BEST ANSWER

https://code.jquery.com/jquery-3.2.1.slim.min.js is the slim edition of jquery, which does not include ajax. slim is the default version included in an Express server. Use the full version of jquery at https://code.jquery.com/jquery-3.2.1.min.js

5
clearshot66 On

JQuery format is wrong:

$(document).ready(function() {
        $('#test').load('doesntmatter');
    });

then add a page name in your directory or such into the load parameters

  • Also make sure your script is the newest functional version
1
Manoher Kumar On

Please try this

$(document).ready(function(){
    $("button").click(function(){
       $("#div1").load("demo_test.txt #p1"); 
    });
});
0
Shreyder On

First of all, make sure you don't include jquery library after your js code! Especially be careful if you use bootstrap!

I had the problem - I didn't see the part of bootstrap code in the footer.php, which had another jquery library - which caused the problem.

0
aezaz vahora On

jQuery ajax load not a function

use below URL
src='https://code.jquery.com/jquery-3.2.1.min.js'

4
Shim-Sao On

Try to not use JQuery for that :

This will ensure that JQuery is loaded before using it.

window.addEventListener("load", function(event) {
  $('#preloader').delay(400).fadeOut(500);
  // Do what you want, the window is entirely loaded and ready to use.
});

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets images. This is in contrast to DOMContentLoaded, which is fired as soon as the page DOM has been loaded, without waiting for resources finish loading.

Mozilla documentation: load event

Edit : According to the question to not confusing window.loaded and jquery.load

First, change jquery.slim to jquery like previous response

Second, use native event handler for best practice (in my opinion) with modern browsers.

// To be sure $ is defined
// Window loaded event
window.addEventListener("load", function(event) {

  // Now $ or JQuery is completly available
  // Now using JQuery.load() should be defined
  $('#test').load('doesntmatter');

  // Do what you want, the window is entirely loaded and ready to use.
});
0
Dexter On

Here's a screenshot of the jquery downloads from the jquery download page (https://jquery.com/download/)
It is easy NOT to notice this line.
Since many people are using jquery ajax, maybe they should rename the file as jquery-slim-no-ajax.js

enter image description here

0
Elvis Esam On

here is good advice, I hade the same issue with jquery .load function and other functions not loading, so ensure you use the same jquery path in every page,(the same version) and it will work perfectly fine.

0
Inês Barata Feio Borges On

I have found this script link to post in your HTML that includes the ajax library:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">

Try it. The load function works.