1. 程式人生 > >頁面可返回到前一頁離開時的瀏覽位置兩種方法

頁面可返回到前一頁離開時的瀏覽位置兩種方法

  • 方法一:簡單粗暴返回前一頁 
1 <a id="aa">News & Events</a>
2 
3 
4 <script>
5 $("#aa").click(function(){
6 history.go(-1)
7 })
8 </script>

 history.back(-1):直接返回當前頁的上一頁,資料全部訊息,是個新頁面
 history.go(-1):也是返回當前頁的上一頁,不過表單裡的資料全部還在

  • 方法二:記錄滾動條位置
 1 $(function () {
2 var str = window.location.href; 3 str = str.substring(str.lastIndexOf("/") + 1); 4 if ($.cookie(str)) { 5 6 $("html,body").animate({ scrollTop: $.cookie(str) }, 1000); 7 } 8 else { 9 } 10 }) 11 12 $(window).scroll(function () { 13 var str = window.location.href; 14 str = str.substring(str.lastIndexOf("/") + 1);
15 var top = $(document).scrollTop(); 16 $.cookie(str, top, { path: '/' }); 17 return $.cookie(str); 18 })

 需要引入檔案

jquery.js
jquery.cookie.js