我必须获取一段包含 document.write 的 JS 片段 - 这会导致 document.write 渲染后页面变为空白。我可以通过重新定义 document.write 并将脚本的响应传递给一个函数来解决这个问题,我用该函数从 getScript() jQuery 函数输出响应。然而,我遇到了稍后在页面中加载的其他元素和脚本的问题,我的重新定义阻止了在页面上的输出。 jQuery getScript() 完成后,如何将 document.write 设置回默认功能?
$.getScript('somescript.com/script/', function(){
//Prevent document.write normal behavior
document.write = function(markup) {
fullMarkup += markup;
appendResponse(markup);
}
}
});
最佳答案
只需将对 document.write
的原始引用保存在某个变量中,并在使用替换后恢复它。
window.backupWrite = document.write;
成功加载(和执行)脚本后
document.write = window.backupWrite;
关于javascript - 如何在 jQuery ajax 完成后启用 document.write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10755244/