繁体   English   中英

为什么document.write()在Firefox和chrome中表现不同?

[英]Why document.write() behaves differently in Firefox and chrome?

我正在制作一个简单的重定向脚本,它将在5秒后将用户重定向到2.html

当我在Chrome上测试脚本并且可以运行时!,但是在最新的Firefox中,它并没有减少几秒钟而挂起。

我是一个初学者,已经尽了最大的努力,但是我无法解决此问题,我在网上看了一下,但是找不到解决方案。 我该如何解决?

我的代码:

index.html的:

<html>
  <head>
    <title>My First Page</title>
    <script type="text/javascript" src="script.js"></script>
  </head>

  <body>

        <input type="button" value=" YES " onclick="yes()" />
  </body>
</html>

的script.js:

c=5;
function yes() {
    alert("Right");
    var ans=0;
    while(ans==0){
        var x=prompt("Your Name?");
        var ans=confirm("Your Name is "+x+" right?");
    }
    document.write('<h1>Welcome '+x+' </h1><p id="show">You are being redirected in 3 seconds</p>');
    function updateShow(){
            document.getElementById('show').innerHTML="<h1>You are being redirected in "+c+"     seconds</h1>";
            c=c-1;
            if(c<0){
                document.location='2.html';
            }
            else{
                 setTimeout(function(){ updateShow(); },1000);
                 }

    }
    var iAmTimer= setTimeout(function(){ updateShow(); },1000);
} 

2.HTML:

<html>
  <body>
    <h1>Welcome</h1>
  </body>
</html>

控制台错误消息

  1. Firefox-无
  2. 铬-无

输出

  1. Firefox(永久):

     Welcome <name> You are being redirected in 3 seconds 

  1. 铬:

     Welcome <name> You are being redirected in 3 seconds Welcome <name> You are being redirected in 2 seconds Welcome <name> You are being redirected in 1 seconds Welcome <name> You are being redirected in 0 seconds 

任何帮助表示赞赏。

您仅应在加载文档时使用document.write()快速插入内容。

根据MDN的文档

在不调用document.open()情况下写入已加载的document.open()将自动执行document.open()调用

document.open()

如果目标中存在文档,则此方法将其清除

因此,在加载文档后使用document.write()会覆盖( 或清除 )您的文档。 由于这些原因, 使用document.write()被认为是不好的做法

运用

document.body.innerHTML+= '<h1>Welcome ' + x + ' </h1><p id="show">You are being redirected in 3 seconds</p>';

而是先将内容隐藏在HTML解决此问题。

演示

另请参见什么是document.write的替代方法?

恕我直言,这在chrome中是如何工作的,我还是个谜,它不应该。

更新:

DOC的

此外,自动document.open()调用发生时document.write()页面加载后调用,但是这不是在W3C规范中定义。

因此,这不再是个谜,因为没有规范,所以不同的浏览器以不同的方式实现它。 避免document.write()另一原因

在功能中使用功能。 这对我有用

c=5;
function yes() {
    alert("Right");
    var ans=0;
    while(ans==0){
        var x=prompt("Your Name?");
        var ans=confirm("Your Name is "+x+" right?");
    }
    document.write('<h1>Welcome '+x+' </h1><p id="show">You are being redirected in 3 seconds</p>');
   updateShow();
    var iAmTimer= setTimeout(function(){ updateShow(); },1000);
} 

 function updateShow(){
            document.getElementById('show').innerHTML="<h1>You are being redirected in "+c+"     seconds</h1>";
            c=c-1;
            if(c<0){
                document.location='2.html';
            }
            else{
                 setTimeout(function(){ updateShow(); },1000);
                 }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM