document.writeln()打破了我的jQuery部分更新。我该如何解决这个问题?

时间:2010-11-18 23:52:10

标签: javascript jquery html firefox

在异步获取请求之后,我无法将下面的HTML写入页面。 HTML由第三方图表服务提供,因此除了在$.get()之后收到回复之外,我无法自行修改。

我刚才问过以下问题:Why is this HTML causing the page to go blank。答案是:

  

页面后面的document.write   完全渲染...将创建一个新的   文件“

因此,我是否可以用jQuery函数替换document.writeln()函数,该函数将包含DOM中的HTML?如果是这样,我需要做什么?

是一个查找和替换的案例?或者它会工作吗?有没有其他方法可以解决这个问题?

<!-- Chart by Corda Enterprise Server (PopChart, Highwire) Version 7.2.3.3177 (Jun 11, 2008) .NET Embedder 7.1.0.2116 -->

<script type="text/javascript">
    var put2087644907Props;
    var put1181439927Props;
    var put433403842Props;

    function getImageMap1325059360() {
        put2087644907Props = new PopUpProperties;
        put2087644907Props.width = 400;
        // lots more of these properties being set 


        // not sure exactly, but this has something to do with providing hover-over functionality
        document.writeln('<map name="imVB_2wFC_WSdl" id="imVB_2wFC_WSdl"><area shape="poly" coords="540,52,547,52,547,59,540,59,540,52" onmouseover="return showPopUp ? showPopUp(put2087644907Props, \'VB_2wFC_WSdl\', \'Schroder All Mats Idx Lnkd Bd I Acc 2.58%\', 543, 49) : false;"/> /* big long string */ onmouseover="return hidePopUp ? hidePopUp() : false;"/></map>');
    }
</script>
<script type="text/javascript">

    document.writeln('<script type="text/javascript" src="http://ie-sv-corda72:2001/?jsPopUp"></scr'+'ipt>');

    if(document.layers) getImageMap1325059360();

</script>
<img id="VB_2wFC_WSdl" name="VB_2wFC_WSdl" width="570" height="243" style="border: 0;"
    usemap="#imVB_2wFC_WSdl" src="http://ie-sv-corda72:2001/?@_CPRVB_2wFC_WSdl" />
<script type="text/javascript">
    if (!document.layers) getImageMap1325059360();
</script>
<noscript>
    <map name="imVB_2wFC_WSdl" id="imVB_2wFC_WSdl">
        <area shape="poly" coords="540,52,547,52,547,59,540,59,540,52" alt="Schroder All Mats Idx Lnkd Bd I Acc 2.58%" />
        <area shape="poly" coords="497,41,504,41,504,48,497,48,497,41" alt="Schroder All Mats Idx Lnkd Bd I Acc 3.27%" />
        <%--lots of these area elements which gives the chart hover-text functionality--%>
    </map>
</noscript>

<!-- Corda Enterprise Server (PopChart, Highwire) Version 7.2.3.3177 (Jun 11, 2008) .NET Embedder 7.1.0.2116 by corda.com -->

作为参考,这是执行get:

的代码
$.get(url, {}, function (result) {

    $('#FactsheetTabs .tab_container').fadeOut(100, function () {
        $('#tabs')
            .append(result) // this fails in FireFox & chrome, but not IE... why?!
            .fadeIn(100);
    });
}, 'html');

如果我编辑回复以注释掉document.writeln()代码,如下所示,则可以:

$.get(url, {}, function (result) {

    $('#FactsheetTabs .tab_container').fadeOut(100, function () {
        result = result.replace("document.writeln", "//document.writeln");
        result = result.replace("document.writeln", "//document.writeln");
        $('#tabs')
            .append(result) // this works now...
            .fadeIn(100);
    });
}, 'html');

1 个答案:

答案 0 :(得分:0)

您可以使用自己的一个覆盖document.writeln()函数,该函数可以安全地将html附加到DOM而不会破坏它。例如:

document.writeln = function (html) {
    $(document.body).append(html);
}

另一个选项接近您的替换方法,但是,不是添加注释分隔符,而是使用您自己的方法替换document.write

result = result.replace(/document\.writeln/g, "$(document.body).append");