2011-03-17 85 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