通过jQuery的load()方法加载整个页面时,JavaScript无法在Safari中执行 [英] JavaScript doesn't execute in Safari when loading entire page via jQuery's load() method
问题描述
我有这两页:
page1.html
http://vidasp.net/tinydemos/jquery-load-issue/page1.html
<!DOCTYPE html>
<html>
<body>
<div id="wrap"> </div>
<script src="jquery.js"></script>
<script> $(function() { $('#wrap').load('page2.html'); }); </script>
</body>
</html>
page2.html
http://vidasp.net/tinydemos/jquery-load-issue/page2.html
<!DOCTYPE html>
<html>
<head>
<script> alert('Page 2 HEAD'); </script>
</head>
<body>
<p> PAGE 2 </p>
<script> alert('Page 2 BODY'); </script>
</body>
</html>
如您所见,我正在将整个page2.html加载到page1.html的#wrap元素中.注意,page2.html包含两个带有alert()函数调用的SCRIPT元素-一个在页面的HEAD中,另一个在页面的BODY中.
As you can see, I am loading the entire page2.html into the #wrap element of page1.html. Notice, that page2.html contains two SCRIPT elements with alert() function calls - one in the HEAD of the page, and the other one in the BODY of the page.
问题:
在Firefox 3.9,IE9 beta,Chrome(最新)和Opera 11中,两个警报都将执行. 在Safari 5中,仅执行第二个警报.
In Firefox 3.9, IE9 beta, Chrome (latest) and Opera 11, both alerts execute. In Safari 5, only the second alert executes.
这是Safari的错误吗?
Is this a Safari bug?
推荐答案
似乎是Safari的问题,但可能不是错误(并且可能在其他浏览器中发生):
Seems like this is a Safari issue, but maybe not a bug (and may happen in other browsers):
jQuery使用浏览器的.innerHTML属性来解析检索到的文档并将其插入到当前文档中.在此过程中,浏览器经常从文档中过滤元素,例如
<html>, <title>, or <head>
元素.结果,由.load()检索的元素可能与由浏览器直接检索的文档不完全相同.
jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as
<html>, <title>, or <head>
elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.
我猜想它适用于任何时候使用.load()
的时间,而不仅仅是在获取片段时.但是我认为最好,因为您正在将内容拉到不包含<head>
标签的<body>
元素中?
I'm guessing that applies to anytime .load()
is used, not just when getting fragments. But I think it would be best, since you are pulling content into a <body>
element that it not include a <head>
tag?
这篇关于通过jQuery的load()方法加载整个页面时,JavaScript无法在Safari中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!