js中网页前进和后退的代码

JavaScript021

js中网页前进和后退的代码,第1张

前进: history.forward()=history.go(1)\x0d\x0a后退: history.back()=history.go(-1)\x0d\x0a注意事项:前进后退按钮是需要在特定环境下才有效果的,即你需要从另一个页面进入到这个页面后,点击这个后退按钮才有后退的效果,否则是没有的。\x0d\x0a【拓展资料】\x0d\x0ajavascript中的后退和刷新;\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a 后退+刷新在C# Web程序中,如为页面按钮写返回上一页代码\x0d\x0athis.RegisterClientScriptBlock("E", "history.go(-2)")\x0d\x0a其中,history.go(-2),要写为-2,因在按钮事件触发前,已刷新一次页面,所以应是-2。\x0d\x0aResponse.Write("history.go(-2)")\x0d\x0a此处也要写为“-2”。跟直接写脚本的有所不同。history.back()是会上一页\x0d\x0ai=1\x0d\x0ahistory.go(i)去指定的某页\x0d\x0a如果是history.go(0)那就是刷新这两个属于JS代码,相当于IE的前进、后退功能。\x0d\x0a具体的用处就要看什么时候需要这个就用上。比如用户注册时的验证是后台验证,不符合要求的时候就可以用这个,可以最大限度保证用户少重复输入数据。\x0d\x0a例如:载入页面:\x0d\x0afunction onLoadPage(){\x0d\x0aif(event.srcElement.tagName=="SPAN"){\x0d\x0aoFrame=top.window.middle.frames[2]\x0d\x0aoTxt=event.srcElement.innerText\x0d\x0aswitch(oTxt){\x0d\x0acase "前 进":\x0d\x0aoFrame.history.go(1)\x0d\x0acase "后 退":\x0d\x0aoFrame.history.back()\x0d\x0acase "刷 新":\x0d\x0aoFrame.location.reload()\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a打开一个jsp页面,肯定是用客户端脚本进行刷新了。\x0d\x0aJavascript刷新页面的几种方法:\x0d\x0a1 history.go(0)\x0d\x0a2 location.reload()\x0d\x0a3 location=location\x0d\x0a4 location.assign(location)\x0d\x0a5 document.execCommand('Refresh')\x0d\x0a6 window.navigate(location)\x0d\x0a7 location.replace(location)\x0d\x0a8 document.URL=location.href

当一条会话历史记录被执行的时候将会触发页面显示(pageshow)事件。(这包括了后退/前进按钮操作)

一般使用方法是通过 addEventListener

但是有时这种写法没有效果,使用下面写法代替