java web的一些基本操作(三) --页面刷新

来源:互联网 发布:上海 对日程序员 招聘 编辑:程序博客网 时间:2024/06/13 17:08

Javascript刷新页面的几种方法 :

1.   history.go(0)   2.   location.reload()   3.   location=location   4.   location.assign(location)   5.   document.execCommand( 'Refresh ')   6.   window.navigate(location)   7.   location.replace(location)   8.document.URL=location.href

区别window.location.Reload()和window.location.href=window.location.href;

<a onclick="javascript:window.location.href=window.location.href;"><a onclick="javascript:window.location.reload();">

测试效果一样。表单没有提交。

<input type="submit" onclick="javascript:window.location.reload();" value="单击" id="btnVCode" /><input type="submit" onclick="javascript:window.location.href=window.location.href;" value="单击" id="btnVCode" />

都提交数据
window.location.Reload()应该是刷新.(如果有数据提交的话,会提示是否提交的(是和否选项))
window.location.href=window.location.href; 是定向url提交数据
最好不要用location.reload(),而用location=location比较好,还有在模式窗口(showModalDialog和showModelessDialog)前者不能用。

reload参数有true和false,比较有意思?

2个方法都是刷新父窗口,但是其中还是有奥妙的哦。
window.opener.location.reload();这个方法在强迫父窗口的时候,在有些IE浏览器(比如安全设置高)的情况下,会弹出一个确认对话框,提示是不是要重新再刷新一次页面,这可是比较郁闷的事情哦,我后来把这个方法替换成了window.opener.location.href=window.opener.location.href;
就不会出现那样的问题了。


在 ,这样在子窗口销毁的时候自动刷新主窗口


“前进” 与“后退”

     function   back()                       {                               window.history.back()                       }                       function   forward()                       {                               window.history.forward()                       }                       function   back2()                       {                               window.history.go(-2)                       }                       function   forward2()                       {                               window.history.go(2)                       } 

js刷新窗口:window.location.replace(window.location.href)

window.location.reload();
window.history.go(0);
window.location.assign(window.location.href)

window.location.href和window.location.replace的区别:
1.html -> 2.html -> 3.html
系统从1.html到2.html后,2中有两个按钮,btn1是window.location.href=3.html, btn2是window.location.replace(3.html), 当点击btn1后在3中用window.history.go(-1);window.history.back();返回的是1.html,

而点击btn2进入3时,用window.history.go(-1);wondow.history.back();返回的是2.html

window.location.href和window.location.replace的亲身体验与区别
当用window.location.href是使用window.history.go(-1)或window.history.back()是管用的
当用window.location.replace(‘url’)是使用window.history.go(-1)或window.history.back()是不管用的
window.location.replace(“3.jsp”);是不向服务器发送请求的跳转
window.history.go(-1);window.history.back();方法是根据服务器记录的请求决定该跳到哪个页面的
window.location.href(“3.jsp”);是向服务器发送请求的跳转,window.history.go(-1);wondow.history.back();方法是根据服务器记录的请求决定该跳到哪个页面的

有3个jsp页面(1.jsp, 2.jsp, 3.jsp),进系统默认的是1.jsp ,当我进入2.jsp的时候, 2.jsp里面用window.location.replace(“3.jsp”);与用window.location.href(“3.jsp”);从用户界面来看是没有什么区别的,但是当3.jsp页面有一个“返回”按钮,调用window.history.go(-1);wondow.history.back();方法的时候,一点这个返回按钮就要返回2.jsp页面的话,区别就出来了,当用window.location.replace(“3.jsp”);连到3.jsp页面的话,3.jsp页面中的调用window.history.go(-1);wondow.history.back();方法是不好用的,会返回到1.jsp 。当用window.location.href(“3.jsp”);连到3.jsp页面的话,3.jsp页面中的调用window.history.go(-1);wondow.history.back();方法是好用的,会返回2.jsp。因为window.location.replace(“3.jsp”);是不向服务器发送请求的跳转,而window.history.go(-1);wondow.history.back();方法是根据服务器记录的请求决定该跳到哪个页面的,所以会跳到系统默认页面1.jsp 。window.location.href(“3.jsp”);是向服务器发送请求的跳转,window.history.go(-1);wondow.history.back();方法是根据服务器记录的请求决定该跳到哪个页面的,所以就可以返回到2.jsp。

<input   type=button   value=刷新   onclick="window.location.reload()">    <input   type=button   value=前进   onclick="window.history.go(1)">    <input   type=button   value=后退   onclick="window.history.go(-1)">    <input   type=button   value=前进   onclick="window.history.forward()">    <input   type=button   value=后退   onclick="window.history.back()">

后退+刷新

<input   type=button   value=后退   onclick="window.history.go(-1);window.location.reload()">

在C# Web程序中,如为页面按钮写返回上一this.RegisterClientScriptBlock(“E”, ““);history.go(-2),要写为-2,因在按钮事件触发前,已刷新一次页面,所以应是-2。

Response.Write("<script language=javascript>history.go(-2);</script>");

此处也要写为“-2”。跟直接写脚本的有所不同。

history.back()是会上一页
i=1
history.go(i)去指定的某夜
如果是history.go(0)那就是刷新
这两个属于JS代码,相当于IE的前进、后退功能。
具体的用处就要看什么时候需要这个就用上。比如用户注册时的验证是后台验证,不符合要求的时候就可以用这个,可以最大限度保证用户少重复输入数据。
例如:载入页面:

function onLoadPage(){    if(event.srcElement.tagName=="SPAN"){        oFrame=top.window.middle.frames[2];        oTxt=event.srcElement.innerText;        switch(oTxt){             case "前 进":                oFrame.history.go(1);            case "后 退":                oFrame.history.back();            case "刷 新":                oFrame.location.reload();        }    }}

1,reload 方法,该方法强迫浏览器刷新当前页面。
语法:location.reload([bForceGet])
参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前页。true, 则以 GET 方式,从服务端取最新的页面, 相当于客户端点击 F5(“刷新”)
2,replace 方法,该方法通过指定URL替换当前缓存在历史里(客户端)的项目,因此当使用replace方法之后,你不能通过“前进”和“后退”来访问已经被替换的URL。
语法: location.replace(URL)
通常使用: location.reload() 或者是 history.go(0) 来做。
此方法类似客户端点F5刷新页面,所以页面method=”post”时,会出现”网页过期”的提示。
因为Session的安全保护机制。
当调用 location.reload() 方法时, aspx页面此时在服务端内存里已经存在, 因此必定是 IsPostback 的。
如果有这种应用: 需要重新加载该页面,也就是说期望页面能够在服务端重新被创建,期望是 Not IsPostback 的。
这里,location.replace() 就可以完成此任务。被replace的页面每次都在服务端重新生成。
代码: location.replace(location.href);

返回并刷新页面:
location.replace(document.referrer);
document.referrer //前一个页面的URL
不要用 history.go(-1),或 history.back();来返回并刷新页面,这两种方法不会刷新页面。
附:
Javascript刷新页面的几种方法:
复制代码 代码如下:

1,history.go(0)
2,location.reload()
3,location=location
4,location.assign(location)
5,document.execCommand(‘Refresh’)
6,window.navigate(location)
7,location.replace(location)
8,document.URL=location.href
自动刷新页面的方法:
1,页面自动刷新:把如下代码加入区域中
复制代码 代码如下:


其中20指每隔20秒刷新一次页面.
2,页面自动跳转:把如下代码加入区域中
复制代码 代码如下:

<meta http-equiv="refresh" content="20;url=http://www.jb51.net">其中20指隔20秒后跳转到http://www.jb51.net页面3,页面自动刷新js版复制代码 代码如下:<script language="JavaScript">function myrefresh(){   window.location.reload();}setTimeout('myrefresh()',1000); //指定1秒刷新一次</script>

4,JS刷新框架的脚本语句
复制代码 代码如下:

//刷新包含该框架的页面用   <script language=JavaScript>   parent.location.reload();</script>//子窗口刷新父窗口<script language=JavaScript>    self.opener.location.reload();</script>( 或 <a href="javascript:opener.location.reload()">刷新</a>   )//刷新另一个框架的页面用   <script language=JavaScript>   parent.另一FrameID.location.reload();</script>如果想关闭窗口时刷新或想开窗时刷新,在<body>中调用以下语句即可。复制代码 代码如下:<body onload="opener.location.reload()"> 开窗时刷新<body onUnload="opener.location.reload()"> 关闭时刷新<script language="javascript">window.opener.document.location.reload()</script>

一、先来看一个简单的例子:

下面以三个页面分别命名为frame.html、top.html、bottom.html为例来具体说明如何做。
frame.html 由上(top.html)下(bottom.html)两个页面组成,代码如下:
复制代码 代码如下:

<HTML> <HEAD> <TITLE> frame </TITLE> </HEAD> <frameset rows="50%,50%"> <frame name=top src="top.html"> <frame name=bottom src="bottom.html"> </frameset> </HTML> 

现在假设top.html (即上面的页面) 有七个button来实现对bottom.html (即下面的页面) 的刷新,可以用以下七种语句,哪个好用自己看着办了。
top.html 页面的代码如下:
复制代码 代码如下:

<HTML> <HEAD> <TITLE> top.html </TITLE> </HEAD> <BODY> <input type=button value="刷新1" onclick="window.parent.frames[1].location.reload()"><br> <input type=button value="刷新2" onclick="window.parent.frames.bottom.location.reload()"><br> <input type=button value="刷新3" onclick="window.parent.frames['bottom'].location.reload()"><br> <input type=button value="刷新4" onclick="window.parent.frames.item(1).location.reload()"><br> <input type=button value="刷新5" onclick="window.parent.frames.item('bottom').location.reload()"><br> <input type=button value="刷新6" onclick="window.parent.bottom.location.reload()"><br> <input type=button value="刷新7" onclick="window.parent['bottom'].location.reload()"><br> </BODY> </HTML>

下面是bottom.html页面源代码,为了证明下方页面的确被刷新了,在装载完页面弹出一个对话框。
复制代码 代码如


bottom.html

This is the content in bottom.html.


一下:
复制代码 代码如下:

1.window指代的是当前页面,例如对于此例它指的是top.html页面。
2.parent指的是当前页面的父页面,也就是包含它的框架页面。例如对于此例它指的是framedemo.html。
3.frames是window对象,是一个数组。代表着该框架内所有子页面。
4.item是方法。返回数组里面的元素。
5.如果子页面也是个框架页面,里面还是其它的子页面,那么上面的有些方法可能不行。
附:
Javascript刷新页面的几种方法:
1 history.go(0)
2 location.reload()
3 location=location
4 location.assign(location)
5 document.execCommand(‘Refresh’)
6 window.navigate(location)
7 location.replace(location)
8 document.URL=location.href

二、自动刷新页面
1.页面自动刷新:把如下代码加入区域中

其中20指每隔20秒刷新一次页面.
2.页面自动跳转:把如下代码加入区域中
http://www.jb51.net“>
其中20指隔20秒后跳转到http://www.jb51.net页面
3.页面自动刷新js版

        0        0           
原创粉丝点击