简体   繁体   English

为什么javascript document.write在Firefox上不起作用?

[英]why does javascript document.write not work on Firefox?

i have an html page displaying an image. 我有一个显示图像的html页面。 it is supposed to delay, change to the video, then after the video plays, change back to the image. 应该延迟一下,然后切换到视频,然后在视频播放后再切换回图像。 this is the javascript i am using: 这是我正在使用的javascript:

<script type="text/javascript">
function playVideo(){
var str='**html of the video object**';
document.open();
document.write(str);
document.close();
}
function backToImage(){
var str='**html of the image**';
document.open();
document.write(str);
document.close();
}
setTimeout("playVideo()",1000);
setTimeout("backToImage()",3000);

</script>

this javascript works in Chrome and Safari. 此javascript可在Chrome和Safari中使用。 It mostly works in IE (the second timeout does not work, but i just discovered this). 它主要在IE中起作用(第二次超时不起作用,但我只是发现了这一点)。 It does not work at all in Firefox. 在Firefox中根本不起作用。 There is no delay, the video simply begins playing and i never see the image; 没有延迟,视频只是开始播放,而我却看不到图像; before or after. 之前或之后。

any thoughts on this would be great. 任何对此的想法将是巨大的。

edit: so it seems that document.write is to blame. 编辑:所以似乎document.write是罪魁祸首。 changing title to reflect this. 更改标题以反映这一点。

in case my original question was not clear, what i am looking for is to replace the image with the video, and then replace the video with the image. 万一我的原始问题不清楚,我要寻找的是用视频替换图像,然后用图像替换视频。 this is all loading in an iframe, so i need to use document.write (or something like it) to actually change the html. 这都是在iframe中加载的,所以我需要使用document.write(或类似的东西)来实际更改 html。

Using document.write after the page has already loaded is a little weird. 页面加载后使用document.write有点奇怪。 That should only really be used for generating a page as it loads. 那应该只真正用于在加载页面时生成页面。 Try something like this instead: 尝试这样的事情:

<html>
  <head>
    <script type="text/javascript">

      function playVideo(){
        var str='**html of the video object**';
        document.getElementById('video-placeholder').innerHTML = str;
      }
      function backToImage(){
        var str='**html of the image**';
        document.getElementById('image-placeholder').innerHTML = str;
      }
      setTimeout(playVideo, 1000);
      setTimeout(backToImage, 3000);

    </script>
  </head>
  <body>
    <div id="video-placeholder"></div>
    <div id="image-placeholder"></div>
  </body>
</html>

The most likely issue, is the opening/writing and closing of a document completely clears it, clearing the current timeouts, in the process, as well. 最可能的问题是,打开/写入和关闭文档会完全清除它,并清除进程中的当前超时。

As pointed in Document Object Model HTML 文档对象模型HTML中所述

open 打开
Open a document stream for writing. 打开文档流进行写入。 If a document exists in the target, this method clears it. 如果目标中存在文档,则此方法将其清除

This is how firefox implemented the clear part ( a complete clearing ) 这就是firefox实施清除部分( 完整清除 )的方式

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

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