2011-03-17 87 views
0

我有這個按鈕刷新頁面onclick刷新但不是從緩存?

echo '<input type="button" value="Reload Page" 
       onClick="window.location.href = window.location.pathname;">'; 

是什麼,以確保該頁面不會從緩存中讀取的最簡單的方法?要使用Math.random()喜歡的東西

echo '<input type="button" value="Reload Page" 
onClick="window.location.href = window.location.pathname&\'?t=\'&Math.random();">'; 

回答

1

使用的Math.random()將工作,所以會(新的Date())。的getTime()也適用。

+0

那是什麼語法?我可以使用'window.location.pathname'嗎? – Radek 2011-03-17 03:42:48

1

這樣做的最可靠的方法是通過服務器端,通過設置標題:

Cache-Control: no-cache 

this文章的更多細節。

+0

有時我必須點擊10x才能看到在添加META HTTP EQUIV =「Pragma」CONTENT =「no-cache」後添加到我的.php文件中的更改> ' – Radek 2011-03-17 04:10:21

0

,而不是JavaScript的,爲什麼不直接鏈接

<?php 
echo "<a href='" . $_SERVER['PHP_SELF'] . "&=" . time() . "'>Reload Page</a>"; 
?> 

呢?

編輯::如我注意,我只建議這個,因爲你在原始帖子中包含示例php。

+0

如果我把我的網址'index.php /「onclick =」alert('xss')'怎麼辦? – alex 2011-03-17 01:09:16