jQuery .load() does not execute scripts when called with a selector in the URL
|Reported by:||Pointy||Owned by:||Pointy|
The ".load()" API always removes script blocks from the markup loaded into the document. However, when called with a plain URL lacking a suffixed selector expression, the loaded content is handed to the ".html()" API before scripts are removed, and that code takes care to save and execute the stripped-out script blocks before throwing them away. When called with a selector expression appended to the URL, the scripts are stripped out before the DOM is updated and so are never executed.
Note that in both cases I'm talking about script blocks within the content actually loaded into the target DOM. In the first case, that's obvious, because the content returned from the xhr is always loaded in its entirety. In the second case, that of there being a selector at the end of the URL, I'm talking about script blocks within the response portion chosen by the selector.
This is not a subtle issue and it's trivial to demonstrate by example, and the cause is quite obvious in the "load()" code. It may be that there's a good reason for the difference in behavior, but there's no mention of how scripts are handled by "load()" in the documentation.
Change History (10)
comment:1 Changed 6 years ago by addyosmani
- Owner set to Pointy
- Priority set to low
- Status changed from new to pending
comment:4 Changed 6 years ago by SlexAxton
- Keywords script execution load added
- Milestone changed from 1.4.3 to 1.5
- Status changed from new to open
comment:9 Changed 6 years ago by timmywil
- Keywords needsdocs added
- Resolution set to wontfix
- Status changed from open to closed
- Version changed from 1.4.2 to 1.6b1