2012-01-02 54 views
1

以下代碼僅適用於Chrome。我需要它在所有主流瀏覽器上運行。 Firefox和IE加載圖像一次。 Opera根本不加載它。如何使此JavaScript代碼便攜?

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> 
+0

jQuery的minified的是30KB。那真的是「太大」了嗎? – darioo 2012-01-02 13:56:03

+1

也許不是太大,但在2分鐘內太大瞭解 – rene 2012-01-02 13:58:33

+1

argh - setTimeout的字符串參數!它應該是'setTimeout(refresh,500)'。 – Alnitak 2012-01-02 13:59:15

回答

4

可以追加隨機數源端,使圖像被重新請求每次:

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

'Cache-Control'頭部應該是沒有必要的。 – 2012-01-02 14:01:13

+0

是的,但如果您始終設置相同的src,瀏覽器將不會再次請求文件。它保持不變 – 2012-01-02 15:21:33