Ken*_*ing 61 javascript jquery
我有一个字符串,看起来像:
<html><head><title>example</title></head><body>some example text</body></html>
Run Code Online (Sandbox Code Playgroud)
我将此字符串作为结果返回给AJAX请求.
我希望浏览器呈现并显示该字符串.想法是做类似的事情:
$('html').parent().html(myString);
Run Code Online (Sandbox Code Playgroud)
嗯,这不起作用.我试图使用IFRAME,但我还没弄明白如何让它工作.
注意:我不可能更改此字符串.我也不可能在随后的服务器调用中重新生成此字符串(否则我只能将浏览器重定向到该URL).
Jon*_*cto 127
document.open/write/close方法将执行您想要的操作:
var newDoc = document.open("text/html", "replace");
newDoc.write(myString);
newDoc.close();
Run Code Online (Sandbox Code Playgroud)
除非传入replace参数,否则document.open调用会添加页面历史记录.因此,用户必须再次单击两次才能转到上一页.
Mar*_*ius 16
您可以删除html标记,然后将所有内容放在html元素中:
$('html').html(myString.replace(/<html>(.*)<\/html>/, "$1"));
Run Code Online (Sandbox Code Playgroud)
至少在firefox(47.0)解决方案中:
var newDoc = document.open("text/html", "replace");
newDoc.write(response);
newDoc.close();
Run Code Online (Sandbox Code Playgroud)
由于按下firefox上的后退按钮仍然加载了以前的历史记录条目 - 即使用"替换"的整个点是为了避免用户单击其后退按钮仅在最后一个页面的视图之前受到欢迎调用document.write()的时间.这样做不会导致上述影响的方法就是直接调用文档对象上的方法:
document.open("text/html", "replace");
document.write(response);
document.close();
Run Code Online (Sandbox Code Playgroud)
使用replace选项不仅可以避免使用垃圾填充用户历史记录,还可以帮助处理浏览器经常处理javascript创建的历史记录条目的奇怪方式所引起的问题,因为有时允许浏览器记录所做的更改处理后退/前进操作(例如,在对其历史记录还原的文档执行document.write()之后,将"wyciwyg://(somenumber)"添加到url中,可能会在历史记录中通过javascript对文档进行处理到以前的状态).
| 归档时间: |
|
| 查看次数: |
53064 次 |
| 最近记录: |