|
Mise à jour : 07/12/2010
Difficulté :
Intermédiaire
6 849 visites depuis 7 jours,
dont 152 sur ce chapitre
classé 33/786
|
![]() Méthode | ![]() Internet Explorer | ![]() Firefox | ![]() Opera | ![]() Google Chrome | ![]() Safari |
|---|---|---|---|---|---|
| DOM Storage | Oui via Persistence avant IE8 |
Oui |
Oui |
Oui |
Oui |
1 2 3 | sessionStorage["user"] = "Thunderseb"; alert(sessionStorage["user"]); // Affiche Thunderseb |
1 2 3 | if (window.sessionStorage) { // OK } |
1 2 3 | localStorage["user"] = "Thunderseb"; alert(localStorage["user"]); // Affiche Thunderseb |
1 2 3 | <style type="text/css"> .storeuserData { behavior: url(#default#userData); } </style> |
1 2 3 4 5 6 7 8 9 10 | // Création de l'objet oPersist var oPersist = document.getElementById("oPersistInput"); // Ecriture oPersist.setAttribute("user", oPersist.value); oPersist.save("oXMLBranch"); // Lecture oPersist.load("oXMLBranch"); alert(oPersist.getAttribute("user")); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Techniques AJAX - Persistence - userData</title>
<style type="text/css">
.storeuserData { behavior: url(#default#userData); }
</style>
<script type="text/javascript">
function fnSaveInput(){
var oPersist = document.getElementById("oPersistInput");;
oPersist.setAttribute("user", oPersist.value);
oPersist.save("userBranch");
}
function fnLoadInput(){
var oPersist = document.getElementById("oPersistInput");
oPersist.load("userBranch");
oPersist.value = oPersist.getAttribute("user") || "";
}
</script>
</head>
<body>
<form id="oPersistForm">
<input class="storeuserData" type="text" id="oPersistInput" />
<input type="button" value="Load" onclick="fnLoadInput()" />
<input type="button" value="Save" onclick="fnSaveInput()" />
</form>
</body>
</html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Techniques AJAX - Dom Storage & Persistence</title> <!--[if lte IE 7]> <style> .storeuserData { behavior: url(#default#userData); } </style> <![endif]--> <script type="text/javascript"> function fnSaveInput() { var oPersist = document.getElementById("oPersistInput"); if (window.globalStorage) { var storage = globalStorage['nayi.free.fr']; storage.setItem("contentTest", oPersist.value); } else if (window.ActiveXObject) { oPersist.setAttribute("contentTest", oPersist.value); oPersist.save("oXMLBranch"); } } function fnLoadInput() { var oPersist = document.getElementById('oPersistInput'); if (window.globalStorage) { var storage = globalStorage["nayi.free.fr"]; oPersist.value = storage.getItem("contentTest"); } else if (window.ActiveXObject) { oPersist.load("oXMLBranch"); oPersist.value = oPersist.getAttribute("contentTest"); } } </script> </head> <body> <form id="oPersistForm"> <input class="storeuserData" type="text" id="oPersistInput" /> <input type="button" value="Load" onclick="fnLoadInput()" /> <input type="button" value="Save" onclick="fnSaveInput()" /> </form> </body> </html> |