0

In my project i have a javascript file that contain a variable like this one:

var htmlcode = "<html><body><h1>My First Web Page</h1><p>My first paragraph.</p></body></html>"

i would to know if in javascript exist a command for render my htmlcode variable ina real html page, i think to use a button for open that page.

So many thanks in advance

Manuel Santi
  • 1,106
  • 17
  • 46
  • 1
    Possible duplicate of [Appending HTML string to the DOM](https://stackoverflow.com/questions/7327056/appending-html-string-to-the-dom) – Daniyal Lukmanov Oct 19 '19 at 14:00

1 Answers1

0

Using jQuery, you can use $('html') to select the HTML tag, and change its content using $('html').html("some new content"):

$("#changeHTML").click(function() {
  $('html').html("<body><div style='color:red;'>New sample text</div></body>");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>
  Sample text
  <button id="changeHTML">Click me</button>
</body>
</html>

Using plain Javascript, you can use document.documentElement to select the document's HTML tag (source), and changing this variable's innerHTML should do the trick.

Anis R.
  • 6,656
  • 2
  • 15
  • 37