JS中window.location.href以及window.history的用法(页面返回上一页 刷新与不刷新)
*一.window. location.href(window可加可不加)用法如下:
*
*1.*self.location.href="/url" 当前页面打开URL页面;
*2.*location.href="/url" 当前页面打开URL页面;
注:windows.location.href="/url" 当前页面打开URL页面, 前面三个用法相同。
*3.*this.location.href="/url" 当前页面打开URL页面
*4.*parent.location.href="/url" 在父页面打开新页面
*5.*top.location.href="/url" 在顶层页面打开新页面
注:如果页面中自定义了frame,那么可将parent self top换为自定义frame的名称,效果是在frame窗口打开url地址
此外,window.location.href=window.location.href;和window.location.Reload()和都是刷新当前页面。区别在于是否有提交数据。当有提交数据时,window.location.Reload()会提示是否提交,window.location.href=window.location.href;则是向指定的url提交数据。
二.window.history用法:
*1.*window.history.go(-1); //返回上一页
window.history.go(-2)//返回两个页面
注:window.history.go(参数可正可负,负数表示返回第几页,正数表示前进第几页,也可以使用访问过的URL) ,此外window.history.forward()也表示向前进几页
*2.*window.history.back(); //返回上一页
*3.*window.location.go(-1); //刷新上一页
*4.*window.history.back();location.reload();//强行刷新(返回上一页刷新页面)
实际应用
a 标签
<a href="<a href="javascript :history.back(-1)">返回上一页</a>
或者onClick用法
<a href="javascript :;" onClick="javascript :history.back(-1);">返回上一页</a>
按钮
<input type="button" name="Submit" οnclick="javascript:history.back(-1);" value="返回上一页">
图片
<a href="javascript :;" onClick="javascript :history.back(-1);"><img src="图片路径" border="0" title="返回上一页"></a>
注:以上为收集百度资料进行整合,如有侵权请通知本人删除