我有一个JavaScript函数,它接收一个字符串作为参数。该字符串是整个网页,包括头部。我需要JavaScript用新内容替换整个当前页面,包括头部。
考虑以下HTML文件:
考虑以下HTML文件:
<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.body.innerHTML=NC;
}
function Hi() {
alert("Hello World");
ReplaceContent(newContent);
}
-->
</script>
</head>
<body onload="Hi();">
Original Content
</body>
</html>
在这种情况下,传递的字符串是:<html><head><script language="Javascript">function Hi() {alert("Goodbye World");}</script></head><body onload="Hi();">New Content</body></html>
当然,由于“ReplaceContent”函数只替换了页面的主体部分,而不是头部,因此我永远也看不到“Goodbye World”的警告。
忽略“为什么我想这样做”,如何动态地替换整个页面,包括头部和JavaScript函数?
请记住,“源”HTML(如上面的“newContent”)仅存在于字符串中,它不存在于任何服务器上,因此我无法直接重定向到它。
我需要对上面的“ReplaceContent”进行哪些更改,才能在替换内容后出现“Goodbye World”的警告?请记住,我无法预先知道newContent变量的值!
document.write()
。 - Saiansh Singh