我写了一个用于刷新网页的元标记。现在我只想刷新一次页面。只刷新一次页面的代码是什么。请帮我解决问题...
提前致谢..
使用javascript,您可以设置一个带有“ refreshed
”变量的cookie,并检查它是否已设置,如果没有,则刷新页面。当然,这涉及到相当多的代码来设置和读取 cookie 以及重新加载时要调用的函数。
我的方法是 url vars,然后又是 php 而不是元标记,它会是这样的:
<?php
if($_GET['r'] != 1) header('refresh: 0; url=/index.php?r=1');
?>
这会重新加载在 url 中设置变量的页面,在这种情况下r
为刷新,为 true。所以下次它加载它不会重新加载。它有效,它只是一行代码,它将为您节省一些编码时间并完成工作。
更新:(用户想要它在 asp 中)
应该可以,但我没有尝试过,目前也无法尝试(我在机场)
<%
dim refreshOnce
refreshOnce = request.querystring("r")
if refreshOnce <> 1 then Response.AddHeader "Refresh", "0;URL=/index.php?r=1"
%>
Javascript 解决方案,使用表单字段来存储状态:
<html>
<head>
<script language="javascript">
function init() {
var form = document.getElementById('theform');
var input = form.refreshed;
function reload() {
location.reload(false);
}
function isRefreshed() {
return !!input.value;
}
function doDisplay() {
var el = document.getElementById(isRefreshed() ? 'two' : 'one');
el.style.display = 'block';
}
function conditionalRefresh() {
if (!isRefreshed()) {
input.value = 'true';
setTimeout(reload, 1000);
}
}
doDisplay();
conditionalRefresh();
}
</script>
</head>
<body onload="init();">
<form id="theform">
<input type="hidden" name="refreshed" />
</form>
<div id="one" style="display: none;">
one
</div>
<div id="two" style="display: none;">
two
</div>
</body>
</html>