why does jquery's .load() ignore – Salman A Jun 08 '11 at 05:54

2 Answers2

6

The .load() function purposefully strips out <script> tags from the loaded content. When you give it a plain URL to load, it will execute the scripts after loading the content and adding it to the DOM. However, if you use the trick of adding a selector after the URL in the first argument:

$('#foo').load("http://some.domain.com/blah #special-div");

then it strips the <script> tags but it does not execute them.

Why? I don't know.

Now, please note that loading an entire page from the <html> tag on down into an element of another page is going to result in some sort of Frankenstein monster of a DOM, if a browser will do it at all. Generally, when you use ".load()" to grab fragments of content to update a page, your server should respond with a piece of a page, not the whole thing. The jQuery deal with allowing a selector after the actual URL is intended to let you strip out a chunk of a page, which is really cool, but it has that drawback that the scripts won't be executed in that case.

Andrei
  • 42,814
  • 35
  • 154
  • 218
Pointy
  • 405,095
  • 59
  • 585
  • 614
0

Because, it cannot run the script inside the <SCRIPT> tag. jQuery has .getScript() to call for scripts only. Check here

Starx
  • 77,474
  • 47
  • 185
  • 261
  • You are not necesarily running the script; think of cases such as underscore templates that have scripts with a special type to avoid parsing. – srcspider Oct 30 '13 at 11:54