使用Javascript替换整个页面,包括头部 [英] Replacing Entire Page Including Head Using Javascript

查看:116
本文介绍了使用Javascript替换整个页面,包括头部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传递字符串的Javascript函数。它传递的字符串是整个网页,包括标题。我需要Javascript来替换整个当前页面,头部和所有新内容。



考虑以下HTML文件:

 < html> 
< head>
< script language =Javascript>
var newContent ='< html>< head>< script language =Javascript>功能Hi(){alert(Goodbye World);}< ; / script>< / head>< body onload =Hi();>新内容< / body>< / html>;
函数ReplaceContent(NC){
document.body.innerHTML = NC;
}
函数Hi(){
alert(Hello World);
ReplaceContent(newContent);
}
- >
< / script>
< / head>
< body onload =Hi();>
原创内容
< / body>
< / html>

在这种情况下,传递的字符串是:

 < html>< head>< script language =Javascript>功能Hi(){alert(Goodbye World);}< / script>< ; / head>< body onload =Hi();>新内容< / body>< / html> 

但是当然,由于ReplaceContent函数只替换了body,而不是header,我从来没有得到再见世界警报。



忽略我为什么要这样做,如何动态替换整个页面,包括头文件和javascript函数?请注意,sourcehtml(上面的'newContent')只存在于字符串中,它不存在于服务器,所以我不能只重定向到它。



一旦内容被替换,我对上面的替换内容做出了什么改变,导致再见世界警报出现?请注意:我不能提前知道newContent变量的值!!

请注意:涉及你为什么想要

解决方案

使用document.write。

 < HTML> 
< head>
< script language =Javascript>
var newContent ='< html>< head>< script language =Javascript>功能Hi(){alert(Goodbye World);}< ; / script>< / head>< body onload =Hi();>新内容< / body>< / html>;
函数ReplaceContent(NC){
document.open();
document.write(NC);
document.close();
}
函数Hi(){
ReplaceContent(newContent);
}
- >
< / script>
< / head>
< body>
原始内容
< a href =javascript:Hi()>替换< / a>
< / body>
< / html>


I have a Javascript function that is passed a string. The string that it is passed is an entire webpage, including the header. I need the Javascript to replace the entire current page, head and all with the new content.

Consider the following HTML file:

<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>

In this case, the passed string is:

<html><head><script language="Javascript">function Hi() {alert("Goodbye World");}</script></head><body onload="Hi();">New Content</body></html>

But of course, since the "ReplaceContent" function is only replacing the body, but not the header, I never get the "Goodbye World" alert.

IGNORING "WHY I WOULD WANT TO DO THIS", How can I dynamically replace the entire page, including the header, and javascript functions?

Please remember the "source" html ('newContent' above) exists only as a string, it does not exist on a server anywhere, so I cannot just redirect to it.

What changes I make to "ReplaceContent" above to cause the "Goodbye World" alert to appear once the content is replaced? Please keep in mind I cannot know in advance the value of the newContent variable!!

Please Note: Answers involving "why would you want to do that" will be downvoted.

解决方案

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="javascript:Hi()">Replace</a>
  </body>
</html>

这篇关于使用Javascript替换整个页面,包括头部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆