admin管理员组文章数量:1313318
I have a every mon page a.html which looks like this:
<html>
<head>
<script type="text/javascript" src="xyz.js" > </script>
</head>
<body>
<div> ... </div>
</body>
</html>
In b.html, I use jquery's .load()
function to a div
.
$("#myDiv").load("a.html")
It works. The xyz.js's content is loaded along with a.html. But why isn't there a <script>
tag? I open firebug to see the source. There is a's but no a's <script>
.
I want the <script>
because I need it to find relative path.
(this question)
Edit: I tried to use .get()
and .html()
. Didn't help.
Edit2: The title is not very appropriate. The xyz.js runs. But no <script>
.
I have a every mon page a.html which looks like this:
<html>
<head>
<script type="text/javascript" src="xyz.js" > </script>
</head>
<body>
<div> ... </div>
</body>
</html>
In b.html, I use jquery's .load()
function to a div
.
$("#myDiv").load("a.html")
It works. The xyz.js's content is loaded along with a.html. But why isn't there a <script>
tag? I open firebug to see the source. There is a's but no a's <script>
.
I want the <script>
because I need it to find relative path.
(this question)
Edit: I tried to use .get()
and .html()
. Didn't help.
Edit2: The title is not very appropriate. The xyz.js runs. But no <script>
.
- possible duplicate of jQuery .load() call doesn't execute javascript in loaded html file – Phil Commented Jun 8, 2011 at 5:01
- Not really. I added the edit2. my javascript is executed. – Lai Yu-Hsuan Commented Jun 8, 2011 at 5:06
-
jQuery's behavior is to strip out
<script>
tags from the loaded document BUT it does download those scripts and eval() them. – Salman Arshad Commented Jun 8, 2011 at 5:54
2 Answers
Reset to default 6The .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./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.
Because, it cannot run the script inside the <SCRIPT>
tag. jQuery has .getScript()
to call for scripts only. Check here
本文标签: javascriptwhy does jquery39s load() ignore ltscriptgtStack Overflow
版权声明:本文标题:javascript - why does jquery's .load() ignore <script>? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741914547a2404647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论