jquery, loading a page while page is not fully loaded

I have a JavaScript file with this line:

$('.item').load('page'); // (tried with document ready)

It does not work. It looks like the line executes before the page is fully loaded (because it works when I type the same line in the console).

Upvotes: 0

Views: 1164

Answers (3)

ThisClark
ThisClark

Reputation: 14823

There are a couple of conditions you need to verify. The path to the document (including file extension) needs to be correct relative to the page that is using this load function. The element you are trying to load into needs to exist.

Are you getting errors in your console? What does it say?

This absolutely works.

<body>
    <div class="item"></div>
    <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.item').load('page.html');
        });
    </script>
</body>

Upvotes: 1

ZZZZtop
ZZZZtop

Reputation: 457

Have you tried waiting until the window is loaded?:

$(window).load(function(){
    $('.item').load('page'); 
});

Upvotes: 0

Kevin Kulla
Kevin Kulla

Reputation: 460

do

 $('document').ready(function(){
        $('.item').load('page');
     });

You need to call it when the page is ready.

Upvotes: 0

Related Questions