Replace the entire page including the Head tag using JavaScript
Today we’ll show you how to replace the entire page including Head tag using JavaScript. There are several ways to replace the page, however in this case we will replace the entire page by using the document.write()
function.
Checkout more articles on JavaScript
Step to replace entire page including Head tag using JavaScript
- Let’s consider the new document as a string.
- Use the
.open()
method which takes two parameters (first is “text/html” and second is “replace”). - If we don’t use the ‘replace’ then method will call adds page history. So we would have to click back two times to go to the previous page. So, replace is necessary argument to pass.
- To this new document use
.write()
method and pass the new document. - Now at last use
.close()
method on the document for it to work.
1 2 3 4 5 6 7 8 | var newHTMLStr = '<!DOCTYPE HTML><html><head><title>JavaScript - '+ 'Clue Mediator</title></head><body style="text-align:center;">'+ '<h2 style="color: #269faf;">Clue Mediator</h2><p>'+ 'The way to write your code</p></body> </html>'; var newHTML = document.open("text/html", "replace"); newHTML.write(newHTMLStr); newHTML.close(); |
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂