Replacing document.write()s in an xhtml+xml page

假装没事ソ 提交于 2019-11-30 06:51:42
var el = document.createElement('div');
el.innerHTML = 'What you used to document.write()';
document.body.appendChild(el);

Note that you'll need to fix the HTML to be valid XHTML, but that should be much less work than converting all the code to use DOM manipulation.

Perhaps you can create an iframe of type text/html, write the content into that, and then import the nodes back into the main page.

We've been using Jquery and set timeouts to rewrite code supplied to us by similar organisations to yours. Here's an example from Search Ignite:

<script>
<!--
// once all the page has loaded
$(document).ready(function ()
    {
        // wait a bit so everything else that runs when the page has loaded loads, then...
        setTimeout(function ()
        {
            // ...load the tracking stuff
            var headerTag = document.getElementsByTagName('head')[0];
            var seo_tag = $.createElement(document.location.protocol + "//track.searchignite.com/si/CM/Tracking/ClickTracking.aspx?siclientid=123456&jscript=1", "script");
            headerTag.appendChild(seo_tag);

        }, 20);
    });
// -->
</script>

The timeout has the added benefit of making our page responsive to the user before the the external code has been loaded by the users browser, very useful if the external supplier's servers ever go down. Yes we loose some tracking stats but the user experience is not compromised.

Obviously you won't be able to rely on JQuery but you get the general idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!