Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么document.write()在Firefox和chrome中的行为不同?_Javascript_Html_Google Chrome_Firefox_Redirect - Fatal编程技术网

Javascript 为什么document.write()在Firefox和chrome中的行为不同?

Javascript 为什么document.write()在Firefox和chrome中的行为不同?,javascript,html,google-chrome,firefox,redirect,Javascript,Html,Google Chrome,Firefox,Redirect,我正在制作一个简单的重定向脚本,在5秒钟后将用户重定向到2.html 当我在Chrome上测试脚本时,它工作了!,但在最新的Firefox中,它并没有减少秒数和挂起时间 我是一个初学者,已经尽了我最大的努力,但我无法解决这个问题,我在网上查找,但无法找到解决方案。我怎样才能解决这个问题 我的代码: index.html: <html> <head> <title>My First Page</title> <script

我正在制作一个简单的重定向脚本,在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);
} 
c=5;
函数yes(){
警惕(“右”);
var ans=0;
while(ans==0){
var x=提示(“您的姓名?”);
var ans=确认(“您的名字是“+x+”对吗?”);
}
document.write('Welcome'+x+'

您将在3秒钟内被重定向)

; 函数updateShow(){ document.getElementById('show').innerHTML=“您在“+c+”秒内被重定向”; c=c-1; 如果(c您应该仅在加载文档时使用插入内容

根据:

在未调用的情况下写入已加载的文档将自动执行调用

和来自:

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

因此,加载文档后使用将覆盖(或清除)您的文档

使用

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

您将在3秒内被重定向;

相反,如果事先将内容隐藏在
HTML
中,则可以解决此问题

另见

这在chrome中是如何工作的,对我来说是个谜,IMHO-它不应该

更新:

发件人:

此外,当在页面加载后调用时,会发生自动调用,但W3C规范中没有定义


所以它不再神秘,因为没有规范,不同的浏览器以不同的方式实现它。还有一个需要避免的原因:)

您在函数中使用函数。这对我很有用

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);
                 }

    }
c=5;
函数yes(){
警惕(“右”);
var ans=0;
while(ans==0){
var x=提示(“您的姓名?”);
var ans=确认(“您的名字是“+x+”对吗?”);
}
document.write('Welcome'+x+'

您将在3秒钟内被重定向)

; updateShow(); var iAmTimer=setTimeout(函数(){updateShow();},1000); } 函数updateShow(){ document.getElementById('show').innerHTML=“您在“+c+”秒内被重定向”; c=c-1;
如果(c@P5Coder您使用的是哪个版本?您是否添加了
Jquery库
?@AsheshKumar:12.0。1@Prashant代码不使用Jquery库。他写道,它在Chrome中工作。它在Firefox(V30)上不工作,在Chrome上也不工作。你能为
document.write()推荐一些替代方案吗
,我知道我可以在网上找到它们,但我知道你是专家。:)嗨,
document.write()
document.write()
,没有确切的替代方法,但关键是-它很少适用于实际场景。你尝试做的事情可以使用
innerHTML
,属性
appendChild(elm)来完成
等等。使用哪个
DOM
操作方法取决于您实际想要做什么……第一句应该是“您应该”还是“您不应该”?因为我很确定您指的是后者。它现在读的是第一句。@Mike'Pomax'Kamermans它是“您应该使用document.write()在加载文档时动态插入内容。”。这是您应该做的。在加载文档后使用它并不理想。啊,对。一句“您应该只[…]”可能会让这一点更加明显。
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.body.innerHTML+= '<h1>Welcome ' + x + ' </h1><p id="show">You are being redirected in 3 seconds</p>';
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);
                 }

    }