简体   繁体   中英

Very Simple jQuery .load Example Not Working

I think this is a very simple question, but I can't seem to get it to work. I need to use JavaScript (specifically jQuery, apparently) to grab some content from a page, and pull it into another page. I've researched this quite a bit, but can't seem to get even a very simple example to work.

Here is the page I'm trying to get content from:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
</head>
<body>
<p>This is the test html page.</p>
</body>
</html>

Here is the page I'm trying to use to pull the content:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>PullData</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</head>
<body>

<ol id="result"></ol>

<script type="text/javascript">
$('#result').load('test.html');
</script>

</body>
</html>

When I open the page it doesn't seem to do anything. I'm basically trying to follow the examples from jquery.com: http://api.jquery.com/load/

Both html pages are in the same folder somewhere on my C drive.

What am I missing?

Thanks in advance for your help!

What browser are you using?

Because of same-origin policy, some browsers won't permit AJAX requests to file:/// URLs, even if the original file was loaded that way. I know this is true of Chrome, but haven't tested others.

What does your .load() error handler say? Oh...

You have your script tags at the end of your page, which means the enclosed JS will be invoked as soon as the browser reaches it, which may not be before the DOM is ready (which means the <ol> might not be set up to get the content of test.html). Try enclosing your load in a $(document).ready() callback as follows:

<script type="text/javascript">
$(document).ready(function() {
    $('#result').load('test.html');
});
</script>

Also why are you inserting a full HTML page into an ordered list? You should try an HTML snippet (no head & body tags) into a content holder such as <div> or <span> where it will be semantically correct.

If none of these things work, attach a callback as follows:

$('#result').load('test.html', null, function(responseText, textStatus, xhr) {
    alert(textStatus); // see what the response status is
});

It seems to make logical sense.

Checking the API on load you may want to see if it actually loads, or if it encoutners an error

$("#result").load("/not-here.php", function(response, status, xhr) {
if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $("#result").html(msg + xhr.status + " " + xhr.statusText);
  }
});

API LINK: http://api.jquery.com/load/

sometimes the debugging information is a good first step to any solution.

Where is the closing script tag?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</head>

Your code needs to be

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>

You must first check if your HTML is ready .

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

To ensure #result1 is loaded, you need a document.ready event handler:

<script type="text/javascript">
$(document).ready(function(){
  $('#result').load('test.html');
});
</script>

Hope this helps. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM