我如何使这个JavaScript代码可移植

How do I make this JavaScript code portable?

本文关键字:代码 可移植 JavaScript 何使这      更新时间:2023-09-26

以下代码仅适用于Chrome浏览器。我需要它能在所有主流浏览器上运行。Firefox和IE只加载一次图像。Opera根本不加载。

screen.jpg文件随Cache-Control: no-cache, no-store发送。我不能使用像jQuery这样的库,因为它们太大了。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
function refresh()
{
    document.getElementById("screen").src = "screen.jpg";
    setTimeout("refresh()", 500);
}
</script>
</head>
<body style="margin:0" onload="refresh()">
<img id="screen" style="width:100%"/>
</body>
</html>

您可以将随机数附加到源的末尾,以便每次重新请求图像:

document.getElementById("screen").src = "screen.jpg?" + Math.random()*999999;