Replacing Entire Page Including Head Using Javascript

前端 未结 5 1613
时光取名叫无心
时光取名叫无心 2020-12-04 23:14

I have a Javascript function that is passed a string. The string that it is passed is an entire webpage, including the header. I need the Javascript to replace the entire

相关标签:
5条回答
  • 2020-12-04 23:50

    Use document.write.

    <html>
      <head>
        <script language="Javascript">
          <!--
          var newContent='<html><head><script language="Javascript">function Hi() {alert("Goodbye World");}</script></head><body onload="Hi();">New Content</body></html>';
          function ReplaceContent(NC) {
            document.open();
            document.write(NC);
            document.close();
          }
          function Hi() {
            ReplaceContent(newContent);
          }
          -->
        </script>
      </head>
      <body>
        Original Content
        <a href="javascript:Hi()">Replace</a>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-04 23:55

    var script = document.createElement('script');
    script.src = 'http://code.jquery.com/jquery-1.7.2.min.js';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);

    0 讨论(0)
  • 2020-12-05 00:00
    $("html").html('your page html here');
    
    0 讨论(0)
  • 2020-12-05 00:04

    Script

    javascript:document.open('text/html');document.write('<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>HAI</title></head><body><h1>OMG HAI2U!!!1</h1></body></html>');document.close();
    

    DOM snapshot of the resulting page

    <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>HAI</title></head><body><h1>OMG HAI2U!!!1</h1></body></html>
    
    0 讨论(0)
  • 2020-12-05 00:10

    document.getElementsByTagName("html")[0].innerHTML Contains both head and body tags. I Usually avoid document.open\write\close

    0 讨论(0)
提交回复
热议问题