Sometimes, we want to replace HTML page with contents retrieved via Ajax with JavaScript.
In this article, we’ll look at how to replace HTML page with contents retrieved via Ajax with JavaScript.
How to replace HTML page with contents retrieved via Ajax with JavaScript?
To replace HTML page with contents retrieved via Ajax with JavaScript, we set the Ajax response to document.body.innerHTML
.
For instance, we write
<html>
<body>
blablabla
<script type="text/javascript">
document.body.innerHTML = "hi!";
</script>
</body>
</html>
to set 'hi!'
as the value of document.body.innerHTML
to update it.
Conclusion
To replace HTML page with contents retrieved via Ajax with JavaScript, we set the Ajax response to document.body.innerHTML
.