用document.write替换页面内容后无法刷新

时间:2018-11-29 21:18:45

标签: javascript html

document.write( newHtml )替换页面内容后,刷新页面仍将新内容保留在Firefox&Edge中,但会从Chrome中的URL重新加载。

以下代码段可用于重现该问题。运行下面的片段,然后单击按钮,它将替换框架中的内容。然后右键单击此处并重新加载框架,您将看到它在Chrome中将按钮带回了,但在Firefox或Edge中没有。

document.getElementById( 'replace-button' ).addEventListener( 'click', ev => {
  var newHtmlContents = '<!doctype html>\
<html>\
  <body>\
    Page Contente Replaced!<br/>\
    Now try refresing the page!\
  </body>\
</html>\
';  
  document.open();
  document.write( newHtmlContents );
  document.close();
});
<!doctype html>
<html>
  <body>
    <button id="replace-button">Replace page contents!</button>
  </body>
</html>

是否有一种解决方法,使页面刷新后即可刷新?我正在构建一个单页应用程序,并且希望像Chrome的示例一样重新使用它来重置页面。

0 个答案:

没有答案