views:

54

answers:

4

I'm just starting with jQuery, and having trouble doing something embarrassingly simple: using .load() to insert external HTML into a main page.

Here's the main page:

<html>
    <head>
    <script src="js/jquery-latest.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">
        $(document).ready(function(){
            $(".log").ajaxError(function() {
                $(this).text('Ajax error.');
            });

            $('.result').load('external.html', function() {
                $('.log').text('Loaded.')
            });
        });
    </script>
    </head>
    <body>
        <div class="log">
        </div>
        <div class="result">
        </div>
    </body>
</html>

And here's external.html:

<p>Some external content!</p>

When I open this page in my browser, "Loaded." is inserted into the .log div, but the external HTML is not inserted into the .result div. Also, even if I delete external.html or rename the file, it still says "Loaded." If the file can't be found, shouldn't that be an Ajax error? It doesn't seem .load() is grabbing the data.

Edit: Based on chrissr's suggestion, I changed the code:

$(".result").load("external.html", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $(".log").html(msg + xhr.status + " " + xhr.statusText);
  } else {
    var msg = "Everything worked fine!";
    $(".log").html(msg);
}
});

When I run this, it inserts "Everything worked fine!" into the .log div.

Edit 2: Just tried it out in Firefox, and it works! (I was using Chrome before). I thought that jQuery works with Chrome, right?

Edit 3: Problem solved! I was opening the file locally (as in file://). I think this was causing security issues or something. Anyway, now I know I just have to throw it on my web server first if I want to test in in chrome.

+2  A: 

Are you sure external.html is present in the same directory as this page?

Adam
Yes, it is present in the same directory.
Alex Miller
+2  A: 

The .load() callback will be executed regardless of success or failure. It just indicates that the load operation has completed (successfully or not).

Try something like this to get details:

$(".result").load("external.html", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $(".log").html(msg + xhr.status + " " + xhr.statusText);
  }
});

I'm guessing you're running into a problem with the same origin policy or that the path to external.html is incorrect.

chrissr
Ah, that would explain why it printed out "Loaded!" However, I copy and pasted your code, and no error message was displayed. I then added an else statement, and it runs the code in the else statement, implying again that there is no error!
Alex Miller
A: 

I copied your HTML files and referenced my local jQuery-1.4.1.min.js file and it worked fine. Like Adam said, you should probably check to see if the script source path and external.html path are correct.

Tim Rourke
Both of these paths are correct. Other jQuery stuff works, so I know my path to that is correct. And external.html is located in the same directory as main.html.
Alex Miller
A: 

Problem solved! I was opening the file locally (as in file://). I think this was causing security issues or something. Anyway, now I know I just have to throw it on my web server first if I want to test in in chrome.

Alex Miller