Note that there are some explanatory texts on larger screens.

plurals
  1. POScripts being stripped with jQuery .load
    primarykey
    data
    text
    <p>I don't think this question has been posed before, at least not in the way I need it answered. I'm using jQuery's .load function. I have a problem when loading just page fragments.</p> <p>When using something like:</p> <pre><code>$('#content').load('loadTest.html'); </code></pre> <p>All of the scripts on loadTest.html load just fine. However, when loading page fragments like this:</p> <pre><code>$('#content').load('loadTest.html #content'); </code></pre> <p>the scripts are stripped out prior to the DOM being updated</p> <p>This is clearly documented in <a href="http://api.jquery.com/load/">http://api.jquery.com/load/</a> which says:</p> <p><strong>Note:</strong> <em>When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is however called with a selector expression appended to the URL, the scripts are stripped out prior to the DOM being updated, which is why they are never executed. An example of both cases can be seen below:</em> </p> <p>I understand that I could just externally load the script which can be used anywhere, but the thing is, I'm using a page-wide ajax system where everything is loaded dynamically. So I don't really feel like having every single javascript function that I'll ever write (100's at this point) in external files. Especially because some of these javascript functions are made from values loaded from a database which I can't account for in a .js file.</p> <p>Is there any workaround for the scripts being stripped out prior to the DOM being updated? Could I in some way manually load them in? Any tiny example would be helpful to me.</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