怎么判断页面是否第一次打开还是刷新

wsldnl 2010-02-27 09:34:12
我的首页只在第一次打开页面的时候弹出一张图片的提示信息
刷新的时候
该页面消失
按后退按钮的时候也消失
...全文
1006 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝色_冰点 2010-02-27
  • 打赏
  • 举报
回复
这跟浏览器的cookie存储方法有关的
关IE完,再重新开就OK了。我用IE8要关掉所有IE才会清掉Cookie
上海程序员3 2010-02-27
  • 打赏
  • 举报
回复
好像判断都不准确的,同一页面打开两次也当成刷新了
蓝色_冰点 2010-02-27
  • 打赏
  • 举报
回复
利用cookie可以实现

这是我封装的一个Cookie操作的Js类,感兴趣的话可以拿去用
function Cookie(key,value)
{
this.key=key;
if(value!=null)
{
this.value=escape(value);
}
this.expiresTime=null;
this.domain=null;
this.path="/";
this.secure=null;
}
Cookie.prototype.setValue=function(value){this.value=escape(value);}
Cookie.prototype.getValue=function(){return unescape(this.value);}

Cookie.prototype.setExpiresTime=function(time){this.expiresTime=time;}
Cookie.prototype.getExpiresTime=function(){return this.expiresTime;}

Cookie.prototype.setDomain=function(domain){this.domain=domain;}
Cookie.prototype.getDomain=function(){return this.domain;}

Cookie.prototype.setPath=function(path){this.path=path;}
Cookie.prototype.getPath=function(){return this.path;}

Cookie.prototype.Write=function(v)
{
if(v!=null)
{
this.setValue(v);
}
var ck=this.key+"="+this.value;
if(this.expiresTime!=null)
{
try
{
ck+=";expires="+this.expiresTime.toUTCString();;
}
catch(err)
{
alert("expiresTime参数错误");
}
}
if(this.domain!=null)
{
ck+=";domain="+this.domain;
}
if(this.path!=null)
{
ck+=";path="+this.path;
}
if(this.secure!=null)
{
ck+=";secure";
}
document.cookie=ck;
}
Cookie.prototype.Read=function()
{
try
{
var cks=document.cookie.split("; ");
var i=0;
for(i=0;i<cks.length;i++)
{
var ck=cks[i];
var fields=ck.split("=");
if(fields[0]==this.key)
{
this.value=fields[1];
return unescape(this.value);
}
}
return null;
}
catch(err)
{
alert("cookie读取错误");
return null;
}
}


页面代码这样写就行了
<html>
<head>
<title>测试</title>
<script type="text/javascript" src="Cookie.js"></script>
<script type="text/javascript" language="javascript">
window.onload=function(){
var ck=new Cookie("HasLoaded");
if(ck.Read()==null){//未加载过,Cookie内容为空
alert("首次打开页面");
ck.Write("true"); //设置Cookie。只要IE不关闭,Cookie就一直存在
}
else{//Cookie存在,表示页面是被刷新的
alert("页面刷新");
}
}
</script>
</head>
<body>
</body>
</html>
ws_hgo 2010-02-27
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
<script type="text/javascript">
function cc()
{
if(window.name=="")
{
AlertMsg();
}
window.name="blueidea";

}
function AlertMsg()
{
var img=document.getElementById("message");
img.style.display="block";
img.style.left="400";
img.style.top="300";
img.onclick=function()
{
img.style.display="none";
}
}

</script>
</head>
<body onload="cc()">
<div id="message" style="display:none; position:absolute;">
<img src='../message.gif'/>
</div>
</body>
</html>
ws_hgo 2010-02-27
  • 打赏
  • 举报
回复
不会友情帮顶!~~~~~~~~~~
wsldnl 2010-02-27
  • 打赏
  • 举报
回复
up~~~~~~~~~~~~~~~~~

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧