在使用ajax在html中加载html文件时出现问题

时间:2012-08-29 11:18:51

标签: javascript ajax cross-browser safari

我使用以下结构将完整的html(见下面的骨架)页面加载到另一个html页面中,点击按钮。

    <!--START:HTML to be loaded by ajax-->
<head>
<!--START: The content inside this head tag is not processed while the page is loaded via ajax-->
    <link rel="stylesheet" type="text/css" href="css/rrr.css" media="screen, projection, print" />
    <script>
        ...
    </script>
    <script type="text/javascript" src="aaas/xxxx.js"></script>
<!--END: The content inside this head tag is not processed while the page is loaded via ajax-->
</head>

<div id="content">
    <!--Page content on which the above script tags inside head tag to act-->
</div>
<!--END:HTML to be loaded by ajax-->

在safari版本5.0.1和5.0.2中,head标记内的内容未被解析,但html中的内容在所有IE,FF和chrome以及safari 5.1.2中被解析。 在id等于“content”的div中的内容在所有浏览器中显示,包括safari 5.0.1和5.0.2。 请帮帮我。谢谢。

2 个答案:

答案 0 :(得分:0)

LINK元素只能用于文档的HEAD。

来自the reference

  

与A不同,它可能只出现在文档的HEAD部分,   虽然它可能会出现任何次数。

因此,您可以在iframe(包含文档)中加载,但不能在div内加载,但不符合规范的浏览器除外。

在您的情况下,最简单的解决方法可能是使用iframe。请注意,css不适用于父文档。

答案 1 :(得分:0)

使用documentFragment to store the responseText,然后执行以下步骤:

  • 删除不应用的CSS
  • 将脚本标记移动到正文
  • 删除head代码
  • 将结果附加到父页面