js通過sessionStorage實現的返回上一頁


通常,我們都會使用比較熟悉的javascript:history.go(-1)來實現返回上一頁,此種方法有一個問題就是返回上一頁后不能刷新當前頁面,給我們的開發帶來了一定的不便,顯然有時這種方法就不是我們需要的。那么有什么辦法能獲取到上一頁的url去刷新上一頁的頁面呢?有人說可以用document.referrer來獲取,這個也是可以的,其定義和用法如下:
referrer屬性可返回載入當前文檔的文檔的URL。
實例:
點擊進入下一頁 a.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>點擊進入下一頁</title>
</head>
<body>
<a href="b.html">點擊進入下一頁</a>
</body>
</html>

返回上一頁 b.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<a href="" id="back">返回上一頁</a> 
<script>
document.getElementById("back").href = document.referrer;
</script> 
</body>
</html>

說明:
經過測試,需要將兩個文件放在服務器中才能得到想要的結果,若直接在本地文件夾中則得到空字符串,若直接在瀏覽器地址欄中輸入b.html的URL地址或使用打開菜單訪問b.html,則document.referrer的值為空字符串。

不過,html5有一個sessionStorage也可以實現這個效果。那么,什么是sessionStorage呢?本文就着重來闡述這個功能。
html5中的Web Storage包括了兩種存儲方式:sessionStorage和localStorage。
sessionStorage用於本地存儲一個會話(session)中的數據,這些數據只有在同一個會話中的頁面才能訪問並且當會話結束后數據也隨之銷毀。因此sessionStorage不是一種持久化的本地存儲,僅僅是會話級別的存儲。
而localStorage用於持久化的本地存儲,除非主動刪除數據,否則數據是永遠不會過期的。

web storage和cookie的區別

Web Storage的概念和cookie相似,區別是它是為了更大容量存儲設計的。Cookie的大小是受限的,並且每次你請求一個新的頁面的時候Cookie都會被發送過去,這樣無形中浪費了帶寬,另外cookie還需要指定作用域,不可以跨域調用。
除此之外,Web Storage擁有setItem,getItem,removeItem,clear等方法,不像cookie需要前端開發者自己封裝setCookie,getCookie。
但是Cookie也是不可以或缺的:Cookie的作用是與服務器進行交互,作為HTTP規范的一部分而存在 ,而Web Storage僅僅是為了在本地“存儲”數據而生。

localStorage和sessionStorage操作

localStorage和sessionStorage都具有相同的操作方法,例如setItem、getItem和removeItem等。

localStorage和sessionStorage的方法

setItem存儲value
用途:將value存儲到key字段
用法:.setItem( key, value)
代碼示例:

sessionStorage.setItem("key", "value"); 	
localStorage.setItem("site", "js8.in");

getItem獲取value
用途:獲取指定key本地存儲的值
用法:.getItem(key)
代碼示例:

var value = sessionStorage.getItem("key"); 	
var site = localStorage.getItem("site");

removeItem刪除key
用途:刪除指定key本地存儲的值
用法:.removeItem(key)
代碼示例:

sessionStorage.removeItem("key"); 	
localStorage.removeItem("site");

clear清除所有的key/value
用途:清除所有的key/value
用法:.clear()
代碼示例:

sessionStorage.clear(); 	
localStorage.clear();

html5 web storage的瀏覽器支持情況

瀏覽器的支持除了IE7及以下不支持外,其他標准瀏覽器都完全支持(ie及FF需在web服務器里運行),值得一提的是IE總是辦好事,例如IE7、IE6中的UserData其實就是javascript本地存儲的解決方案。通過簡單的代碼封裝可以統一到所有的瀏覽器都支持web storage。
要判斷瀏覽器是否支持localStorage可以使用下面的代碼:

if(window.localStorage){     
    alert("瀏覽支持localStorage") 
}else{    
    alert("瀏覽暫不支持localStorage") 
} 
//或者 
if(typeof window.localStorage == 'undefined'){
    alert("瀏覽暫不支持localStorage") 
}

其他操作方法:點操作和[]
web Storage不但可以用自身的setItem,getItem等方便存取,也可以像普通對象一樣用點(.)操作符,及[]的方式進行數據存儲,像如下的代碼:

var storage = window.localStorage; 
    storage.key1 = "hello"; 
    storage["key2"] = "world"; 
    console.log(storage.key1); 
    console.log(storage["key2"]);

localStorage和sessionStorage的key和length屬性實現遍歷
sessionStorage和localStorage提供的key()和length可以方便的實現存儲的數據遍歷,例如下面的代碼:

var storage = window.localStorage; 
for (var i=0, len = storage.length; i  <  len; i++){ 
    var key = storage.key(i);     
    var value = storage.getItem(key);    
    console.log(key + "=" + value); 
}

storage事件

storage還提供了storage事件,當鍵值改變或者clear的時候,就可以觸發storage事件,如下面的代碼就添加了一個storage事件改變的監聽:

if(window.addEventListener){ 	
    window.addEventListener("storage",handle_storage,false); 
}else if(window.attachEvent){ 	
    window.attachEvent("onstorage",handle_storage);
} 
function handle_storage(e){ 	
    if(!e){e=window.event;}	
 }

storage事件對象的具體屬性如下表:

Property Type Description
key String The named key that was added, removed, or moddified
oldValue Any The previous value(now overwritten), or null if a new item was added
newValue Any The new value, or null if an item was added
url/uri String The page that called the method that triggered this change

具體到實際的頁面中,代碼如下:

點擊進入下一頁 index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>點擊進入下一頁</title>
</head>
<body>
	<a href="next.html">點擊進入下一頁</a>
	<script>
		var ss = sessionStorage,
			url = window.location;
		ss.setItem('back',url);
	</script>
</body>
</html>

返回上一頁 next.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>返回上一頁</title>
</head>
<body>
	<a href="" id="back">點擊返回上一頁</a>
	<script>
		var ss = sessionStorage,
			url = ss.getItem('back');
			document.getElementById("back").href = url;
	</script>
</body>
</html>

本文轉自:http://www.cnblogs.com/yuzhongwusan/archive/2011/12/19/2293347.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM