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="https://stackoverflow.com/questions/4292603/javascript:Hi()">Replace</a>
</body>
</html>