93,723 views / 2009.09.01 / 6:06 下午
众所周知,刷新页面可以直接在html的head标签中使用如下代码:
<meta http-equiv="refresh" content="1; URL=http://www.71j.cn"> |
<meta http-equiv="refresh" content="1; URL=http://www.71j.cn">
其中content中的数字1表示等待是时间(秒)
也可以使用php发送头消息:
header("location:http://71j.cn"); |
header("location:http://71j.cn");
甚至可以使用js中window.location.reload()方法,但是我们在开发过程中发现,因为浏览器或者CDN等带有的缓存机制会使页面刷新后,数据得不到刷新。
解决办法如下:
function autorefresh(){
var s="&";
var href=window.location.href;
if(href.indexOf("t=")==-1){
if(href.indexOf("?")==-1) s="?";
window.location.href+=s+"t="+new Date().getTime();
}else{
window.location.href = href.replace(/t=[0-9]+/,"t="+new Date().getTime());
}
} |
function autorefresh(){
var s="&";
var href=window.location.href;
if(href.indexOf("t=")==-1){
if(href.indexOf("?")==-1) s="?";
window.location.href+=s+"t="+new Date().getTime();
}else{
window.location.href = href.replace(/t=[0-9]+/,"t="+new Date().getTime());
}
}
这样给url加上时间戳,可以有效解决本问题。
72,168 views / 2009.09.01 / 6:06 下午
随着Ajax的泛滥,有时需要我们从url获取变量进行用户交互,本函数提供了获取方法:
function getQuery(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]); return null;
} |
function getQuery(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]); return null;
}
如需要从下面地址中获取id的值:
http://71j.cn/a.php?a=my&id=7&m=1
只需要在当前页面调用getQuery(“id”)即可。