Javascript仅适用于Chrome,但没有其他浏览器

时间:2016-11-06 20:51:51

标签: javascript html google-chrome

我试图在页面加载时通过javascript填充背景中的图像。我尝试使用以下代码段 - 我尝试在本地Chrome以及localhost上运行它,但运行正常,但在Firefox,Safari和所有其他浏览器中,图像都不会显示。

var body = document.body,
    html = document.documentElement;

var height = Math.floor(Math.min(body.scrollHeight));
for(i=0; i<height; i++){
  var images = [],
  index = 0;
  images[0] = "<img id='rel' src='http://ia.media-imdb.com/images/M/MV5BMTU4MDU3NDQ5Ml5BMl5BanBnXkFtZTgwOTU5MDUxNTE@._V1_SX300.jpg'>";

  images[1] = "<img id='rel1' src='http://ia.media-imdb.com/images/M/MV5BMTk2NTI1MTU4N15BMl5BanBnXkFtZTcwODg0OTY0Nw@@._V1_SX300.jpg'>";

  images[2] = "<img id='rel2' src='http://ia.media-imdb.com/images/M/MV5BODU4MjU4NjIwNl5BMl5BanBnXkFtZTgwMDU2MjEyMDE@._V1_SX300.jpg'>";

  images[3] = "<img id='rel' src='http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg'>";

  images[4] = "<img id='rel1' src='http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg'>";

  images[5] = "<img id='rel2' src='http://ia.media-imdb.com/images/M/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw@@._V1_SX300.jpg'>";

  images[6] = "<img id='rel' src='http://ia.media-imdb.com/images/M/MV5BMjU4NDExNDM1NF5BMl5BanBnXkFtZTgwMDIyMTgxNzE@._V1_SX300.jpg'>";

  images[7] = "<img id='rel1' src='http://ia.media-imdb.com/images/M/MV5BOTAzODEzNDAzMl5BMl5BanBnXkFtZTgwMDU1MTgzNzE@._V1_SX300.jpg'>";

  images[8] = "<img id='rel2' src='http://ia.media-imdb.com/images/M/MV5BMTc2MTQ3MDA1Nl5BMl5BanBnXkFtZTgwODA3OTI4NjE@._V1_SX300.jpg'>";

  images[9] = "<img id='rel' src='http://ia.media-imdb.com/images/M/MV5BMTAzZmJiOGQtOWM3NS00MDEwLWJiNGEtN2QzMDM3NDJjMWQwXkEyXkFqcGdeQXVyNDUzOTQ5MjY@._V1_SX300.jpg'>";

  images[10] = "<img id='rel1' src='http://ia.media-imdb.com/images/M/MV5BMTUyMTE0ODcxNF5BMl5BanBnXkFtZTgwODE4NDQzNTE@._V1_SX300.jpg'>";

  images[11] = "<img id='rel2' src='http://ia.media-imdb.com/images/M/MV5BMjM2Nzg4MzkwOF5BMl5BanBnXkFtZTgwNzA0OTE3NjE@._V1_SX300.jpg'>";

  images[12] = "<img id='rel' src='http://ia.media-imdb.com/images/M/MV5BMjEyMjcyNDI4MF5BMl5BanBnXkFtZTcwMDA5Mzg3OA@@._V1_SX300.jpg'>";

  images[13] = "<img id='rel1' src='http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg'>";

  images[14] = "<img id='rel2' src='http://ia.media-imdb.com/images/M/MV5BMjIxMjgxNTk0MF5BMl5BanBnXkFtZTgwNjIyOTg2MDE@._V1_SX300.jpg'>";

  index = Math.round(Math.random() * images.length);

  document.write(images[index]);
}

和HTML:

<body>
  <div id="continer">
    <script type="text/javascript" src="images.js"></script>
  </div>

2 个答案:

答案 0 :(得分:0)

在Firefox和其他一些浏览器中,滚动条未附加到正文,但documentElement<html>标记。

你必须检查哪一个实际上有scrollHeight

var body = document.body,
    html = document.documentElement;

var bodyHeight = Math.floor(Math.min(body.scrollHeight));
var htmlHeight = Math.floor(Math.min(html.scrollHeight));

var height = Math.max(bodyHeight, htmlHeight);

for(i=0; i<height; i++){ ...

答案 1 :(得分:0)

因为您的代码是指页面的正文和可视元素,所以您需要在加载页面后执行脚本。我修改了你的代码,它现在可以在Chrome,IE和FF中使用。

最关键的变化是从onLoad()事件运行脚本。

<body  onload="showImages();">

注意为了简洁,我在这里删除了很多图像数组插入。

<html>
<head>
</head>
<script>
function showImages() {
var body = document.body,
    html = document.documentElement;

var height = Math.floor(Math.min(body.scrollHeight));
for(i=0; i<height; i++){
var images = [],
index = 0;
images[0] = "<img id='rel' src='http://ia.media-imdb.com/images/M/MV5BMTU4MDU3NDQ5Ml5BMl5BanBnXkFtZTgwOTU5MDUxNTE@._V1_SX300.jpg'>";

images[1] = "<img id='rel1' src='http://ia.media-imdb.com/images/M/MV5BMTk2NTI1MTU4N15BMl5BanBnXkFtZTcwODg0OTY0Nw@@._V1_SX300.jpg'>";

//  ...

index = Math.round(Math.random() * images.length);

document.write(images[index]);

}
}
</script>
 <body  onload="showImages();">
<div id="continer">``

</div>