$('#something').click(function() {
location.reload();
});
reload()
函数接受一个可选参数,该参数可以设置为true
以强制从服务器而不是缓存重新加载。该参数默认为false
,因此默认情况下,页面可能会从浏览器的缓存中重新加载。
即使没有 jQuery,这应该适用于所有浏览器:
location.reload();
使用 JavaScript 刷新页面有多种无限制的方法:
location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
location.reload(false)
如果我们需要再次从 Web 服务器中提取文档(例如文档内容动态更改),我们会将参数传递为
true
。
您可以继续创作列表:
window.location = window.location
window.self.window.self.window.window.location = window.location
var methods = [
"location.reload()",
"history.go(0)",
"location.href = location.href",
"location.href = location.pathname",
"location.replace(location.pathname)",
"location.reload(false)"
];
var $body = $("body");
for (var i = 0; i < methods.length; ++i) {
(function(cMethod) {
$body.append($("<button>", {
text: cMethod
}).on("click", function() {
eval(cMethod); // don't blame me for using eval
}));
})(methods[i]);
}
button {
background: #2ecc71;
border: 0;
color: white;
font-weight: bold;
font-family: "Monaco", monospace;
padding: 10px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.5s ease;
margin: 2px;
}
button:hover {
background: #27ae60;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>