|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +?> |
| 7 | + |
| 8 | +<script> |
| 9 | + try { |
| 10 | + if (!window.localStorage || !window.sessionStorage) { |
| 11 | + throw new Error(); |
| 12 | + } |
| 13 | + |
| 14 | + localStorage.setItem('storage_test', 1); |
| 15 | + localStorage.removeItem('storage_test'); |
| 16 | + } catch(e) { |
| 17 | + (function () { |
| 18 | + var Storage = function (type) { |
| 19 | + var data; |
| 20 | + |
| 21 | + function createCookie(name, value, days) { |
| 22 | + var date, expires; |
| 23 | + |
| 24 | + if (days) { |
| 25 | + date = new Date(); |
| 26 | + date.setTime(date.getTime()+(days * 24 * 60 * 60 * 1000)); |
| 27 | + expires = '; expires=' + date.toGMTString(); |
| 28 | + } else { |
| 29 | + expires = ''; |
| 30 | + } |
| 31 | + document.cookie = name + '=' + value+expires+'; path=/'; |
| 32 | + } |
| 33 | + |
| 34 | + function readCookie(name) { |
| 35 | + var nameEQ = name + '=', |
| 36 | + ca = document.cookie.split(';'), |
| 37 | + i = 0, |
| 38 | + c; |
| 39 | + |
| 40 | + for (i=0; i < ca.length; i++) { |
| 41 | + c = ca[i]; |
| 42 | + |
| 43 | + while (c.charAt(0) === ' ') { |
| 44 | + c = c.substring(1,c.length); |
| 45 | + } |
| 46 | + |
| 47 | + if (c.indexOf(nameEQ) === 0) { |
| 48 | + return c.substring(nameEQ.length, c.length); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + function setData(data) { |
| 56 | + data = encodeURIComponent(JSON.stringify(data)); |
| 57 | + createCookie(type === 'session' ? getSessionName() : 'localStorage', data, 365); |
| 58 | + } |
| 59 | + |
| 60 | + function clearData() { |
| 61 | + createCookie(type === 'session' ? getSessionName() : 'localStorage', '', 365); |
| 62 | + } |
| 63 | + |
| 64 | + function getData() { |
| 65 | + var data = type === 'session' ? readCookie(getSessionName()) : readCookie('localStorage'); |
| 66 | + |
| 67 | + return data ? JSON.parse(decodeURIComponent(data)) : {}; |
| 68 | + } |
| 69 | + |
| 70 | + function getSessionName() { |
| 71 | + if (!window.name) { |
| 72 | + window.name = new Date().getTime(); |
| 73 | + } |
| 74 | + |
| 75 | + return 'sessionStorage' + window.name; |
| 76 | + } |
| 77 | + |
| 78 | + data = getData(); |
| 79 | + |
| 80 | + return { |
| 81 | + length: 0, |
| 82 | + clear: function () { |
| 83 | + data = {}; |
| 84 | + this.length = 0; |
| 85 | + clearData(); |
| 86 | + }, |
| 87 | + |
| 88 | + getItem: function (key) { |
| 89 | + return data[key] === undefined ? null : data[key]; |
| 90 | + }, |
| 91 | + |
| 92 | + key: function (i) { |
| 93 | + var ctr = 0, |
| 94 | + k; |
| 95 | + |
| 96 | + for (k in data) { |
| 97 | + if (ctr.toString() === i.toString()) { |
| 98 | + return k; |
| 99 | + } else { |
| 100 | + ctr++ |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return null; |
| 105 | + }, |
| 106 | + |
| 107 | + removeItem: function (key) { |
| 108 | + delete data[key]; |
| 109 | + this.length--; |
| 110 | + setData(data); |
| 111 | + }, |
| 112 | + |
| 113 | + setItem: function (key, value) { |
| 114 | + data[key] = value.toString(); |
| 115 | + this.length++; |
| 116 | + setData(data); |
| 117 | + } |
| 118 | + }; |
| 119 | + }; |
| 120 | + |
| 121 | + window.localStorage.__proto__ = window.localStorage = new Storage('local'); |
| 122 | + window.sessionStorage.__proto__ = window.sessionStorag = new Storage('session'); |
| 123 | + })(); |
| 124 | + } |
| 125 | +</script> |
0 commit comments