Note that there are some explanatory texts on larger screens.

plurals
  1. POproblems with jQuery load() method
    primarykey
    data
    text
    <p>I'm just starting with jQuery, and having trouble doing something embarrassingly simple: using .load() to insert external HTML into a main page.</p> <p>Here's the main page:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="js/jquery-latest.min.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt; &lt;script type="text/javascript" charset="utf-8"&gt; $(document).ready(function(){ $(".log").ajaxError(function() { $(this).text('Ajax error.'); }); $('.result').load('external.html', function() { $('.log').text('Loaded.') }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="log"&gt; &lt;/div&gt; &lt;div class="result"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And here's external.html:</p> <pre><code>&lt;p&gt;Some external content!&lt;/p&gt; </code></pre> <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.</p> <p>Edit: Based on chrissr's suggestion, I changed the code:</p> <pre><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); } }); </code></pre> <p>When I run this, it inserts "Everything worked fine!" into the .log div.</p> <p>Edit 2: Just tried it out in Firefox, and it works! (I was using Chrome before). I thought that jQuery works with Chrome, right?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload