Note that there are some explanatory texts on larger screens.

plurals
  1. POVery interesting jQuery load behavior, a bug or solution?
    primarykey
    data
    text
    <p>I was recently trying to find a bug in some scripting and I discovered this very interesting behavior when loading a page with jQuery.</p> <p>File #1: Test1.htm</p> <pre><code>&lt;div id="test"&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $('#test').load('test2.htm #content',function(){ alert('done loading!'); }) }) &lt;/script&gt; </code></pre> <p>File #2: Test2.htm</p> <pre><code>&lt;div id="content"&gt; howdy &lt;script type="text/javascript"&gt; $(document).ready(function(){ alert('hello #1'); }) &lt;/script&gt; &lt;SCRIPT type="text/javascript"&gt; $(document).ready(function(){ alert('hello #2'); }) &lt;/SCRIPT&gt; &lt;/div&gt; </code></pre> <p>Now when I run Test1.htm, I get the following:</p> <ul> <li>hello #2 alert</li> <li>howdy from test2.htm displays</li> <li>done loading alert</li> </ul> <p>As you can see the only difference is the script tag is in upper case for the hello #2 alert. The script to show the hello #1 alert never gets exectued... </p> <p>So far, I've tested this in Firefox 3.55, IE 8.0.6001.18702 and Chrome 3.0.195.33 with similar results.</p> <p>In the past, I've wanted to execture javascript from the loaded content, similar to <a href="https://stackoverflow.com/questions/75943/how-do-you-execute-a-dynamically-loaded-javascript-block">this SO question</a>. So my question is, is this a bug or a solution?</p> <hr> <p>Update: As Jitter states below, the same thing happens if Test2.htm has the script outside the content I am loading.</p> <pre><code>&lt;div id="content"&gt; howdy from test2.htm &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ alert('hello #1'); }) &lt;/script&gt; &lt;SCRIPT type="text/javascript"&gt; $(document).ready(function(){ alert('hello #2'); }) &lt;/SCRIPT&gt; </code></pre>
    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.
 

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