为什么JavaScript的document.write不适用于Firefox? [英] why does javascript document.write not work on Firefox?

查看:130
本文介绍了为什么JavaScript的document.write不适用于Firefox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML页面显示图像。它应该延迟,转换到视频,然后视频播放后,切换回图像。这是我使用的JavaScript:

 < script type =text / javascript> 
函数playVideo(){
var str ='**视频对象**的html';
document.open();
document.write(str);
document.close();

函数backToImage(){
var str ='**图片的html **';
document.open();
document.write(str);
document.close();
}
setTimeout(playVideo(),1000);
setTimeout(backToImage(),3000);

< / script>

这个javascript适用于Chrome和Safari。它主要在IE中工作(第二个超时不起作用,但我刚刚发现这一点)。它在Firefox中完全不起作用。没有延迟,视频只是开始播放,我从来没有看到图像;之前或之后。

对此的任何想法都会很棒。

编辑:所以好像是文件。写就是怪。改变标题,以反映这一点。

如果我原来的问题不清楚,我正在寻找的是用视频取代图像,然后用图片。这是所有加载在iframe,所以我需要使用document.write(或类似的东西)实际改变的HTML。

解决方案在页面已经加载之后使用 document.write 有点奇怪。这应该只用于在加载时生成页面。尝试像这样:

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

函数playVideo(){
var str ='**视频对象的** html';
document.getElementById('video-placeholder')。innerHTML = str;

函数backToImage(){
var str ='**图片的html **';
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>


i have an html page displaying an image. 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:

<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. It mostly works in IE (the second timeout does not work, but i just discovered this). It does not work at all in 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. 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.

解决方案

Using document.write after the page has already loaded is a little weird. 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>

这篇关于为什么JavaScript的document.write不适用于Firefox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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