教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 javascript刷新当前页面方法汇总

javascript刷新当前页面方法汇总

发布时间:2015-06-18   编辑:jiaochengji.com
有关javascript刷新当前页面的方法,js刷新当前页面的多种方法,包括reload方法、location方法与referer方法等,有需要的朋友参考下。

在javascript编程中,刷新当前页面,有如下几种方法供选择:
1,reload方法
2,replace方法
3,history方法

下面我们就这几种刷新当前页面的方法,分别加以介绍。
 
1,reload 方法,该方法强迫浏览器刷新当前页面。
语法:location.reload([bforceget])
参数: bforceget, 可选参数, 默认为 false,从客户端缓存里取当前页。true, 则以 get 方式,从服务端取最新的页面, 相当于客户端点击 f5("刷新")

附,javascript reload函数
location.reload -- 重载当前页面
reload,中文"重新加载"
reload函数语法

location.reload(isServer);
reload函数参数

isServer -- true时从服务器端重新下载页面,否则只从本地浏览器缓存中重载
不使用参数时,默认为false,从本地浏览器缓存中重载

例1:
 

复制代码 代码示例:
<input type="button" value="点击按钮重新加载页面" onclick="location.reload();">
 

从浏览器缓存中重载

例2,
 

复制代码 代码示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript reload() 函数示例</title>  
</head>
<body>
(教程集 www.jiaochengji.com)编辑整理
<input type="button" value="点击按钮重新加载页面" onclick="location.reload();">
</body>
</html>

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();来返回并刷新页面,这两种方法不会刷新页面。

附,location.replace函数
-- 设置当前文档的URL,并且在history对象的地址列表中移除这个URL
replace,中文"替换"
replace函数语法

location.replace(url);
replace函数参数

url -- URL
replace函数说明

replace函数在设置URL方面与location的href属性或assign函数完全一样,但是它会删除history对象的地址列表中的URL,从而使go或back等函数无法导航。

例1:
 

复制代码 代码示例:
<input type="button" value="点击按钮定位到教程集" onclick="location.replace('http://www.jiaochengji.com/');">

例2:
 

复制代码 代码示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript replace() 函数示例</title>  
</head>
<body>
<input type="button" value="点击按钮定位到教程集" onclick="location.replace('http://www.jiaochengji.com/');">
</body>
</html>

有关javascript中location.reload与location.replace的区别,大家有时间可以研究下。

一,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,页面自动刷新:
在<head>区域中加入代码:
 

复制代码 代码示例:
<meta http-equiv="refresh" content="20">
 

其中20指每隔20秒刷新一次页面.

2,页面自动跳转:
在<head>区域中加入代码:
 

复制代码 代码示例:
<meta http-equiv="refresh" content="20;url=http://www.jiaochengji.com">
 

其中20指隔20秒后跳转到http://www.jiaochengji.com页面

3,页面自动刷新js版
 

复制代码 代码示例:
<script language="javascript">
function myrefresh()
{
   window.location.reload();
}
settimeout('myrefresh()',1000); //指定1秒刷新一次
</script>

三,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>

2,若需要关闭窗口时刷新或想开窗时刷新的话,在<body>中调用以下语句即可。
 

复制代码 代码示例:

<body onload="opener.location.reload()"> 开窗时刷新
<body onunload="opener.location.reload()"> 关闭时刷新

<script language="javascript">
window.opener.document.location.reload()
</script>

您可能感兴趣的文章:
javascript刷新当前页面方法汇总
js自动刷新当前页面方法详解
jquery刷新页面代码及js常用函数汇总
js刷新页面方法汇总
asp网页自动刷新方法汇总
js自动刷新页面代码汇总
jQuery 定时局部刷新(setInterval)方法总结
js返回上一页并刷新多种方法举例
js刷新当前页面的几种方法
js返回上一页并刷新的多种方法

[关闭]
~ ~