* Initializing storage for the custom state's object
()
| 519 | * Initializing storage for the custom state's object |
| 520 | */ |
| 521 | function storageInitialize() { |
| 522 | var sessionStorage; |
| 523 | /** |
| 524 | * sessionStorage throws error when cookies are disabled |
| 525 | * Chrome content settings when running the site in a Facebook IFrame. |
| 526 | * see: https://github.com/devote/HTML5-History-API/issues/34 |
| 527 | * and: http://stackoverflow.com/a/12976988/669360 |
| 528 | */ |
| 529 | try { |
| 530 | sessionStorage = global['sessionStorage']; |
| 531 | sessionStorage.setItem(sessionStorageKey + 't', '1'); |
| 532 | sessionStorage.removeItem(sessionStorageKey + 't'); |
| 533 | } catch(_e_) { |
| 534 | sessionStorage = { |
| 535 | getItem: function(key) { |
| 536 | var cookie = document.cookie.split(key + "="); |
| 537 | return cookie.length > 1 && cookie.pop().split(";").shift() || 'null'; |
| 538 | }, |
| 539 | setItem: function(key, value) { |
| 540 | var state = {}; |
| 541 | // insert one current element to cookie |
| 542 | if (state[windowLocation.href] = historyObject.state) { |
| 543 | document.cookie = key + '=' + JSON.stringify(state); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | try { |
| 550 | // get cache from the storage in browser |
| 551 | stateStorage = JSON.parse(sessionStorage.getItem(sessionStorageKey)) || {}; |
| 552 | } catch(_e_) { |
| 553 | stateStorage = {}; |
| 554 | } |
| 555 | |
| 556 | // hang up the event handler to event unload page |
| 557 | addEvent(eventNamePrefix + 'unload', function() { |
| 558 | // save current state's object |
| 559 | sessionStorage.setItem(sessionStorageKey, JSON.stringify(stateStorage)); |
| 560 | }, false); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * This method is implemented to override the built-in(native) |