[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/history-hash-ie/ -> history-hash-ie-debug.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('history-hash-ie', function (Y, NAME) {
   9  
  10  /**
  11   * Improves IE6/7 support in history-hash by using a hidden iframe to create
  12   * entries in IE's browser history. This module is only needed if IE6/7 support
  13   * is necessary; it's not needed for any other browser.
  14   *
  15   * @module history
  16   * @submodule history-hash-ie
  17   * @since 3.2.0
  18   */
  19  
  20  // Combination of a UA sniff to ensure this is IE (or a browser that wants us to
  21  // treat it like IE) and feature detection for native hashchange support (false
  22  // for IE < 8 or IE8/9 in IE7 mode).
  23  if (Y.UA.ie && !Y.HistoryBase.nativeHashChange) {
  24      var Do          = Y.Do,
  25          GlobalEnv   = YUI.namespace('Env.HistoryHash'),
  26          HistoryHash = Y.HistoryHash,
  27  
  28          iframe = GlobalEnv._iframe,
  29          win    = Y.config.win;
  30  
  31      /**
  32       * Gets the raw (not decoded) current location hash from the IE iframe,
  33       * minus the preceding '#' character and the hashPrefix (if one is set).
  34       *
  35       * @method getIframeHash
  36       * @return {String} current iframe hash
  37       * @static
  38       */
  39      HistoryHash.getIframeHash = function () {
  40          if (!iframe || !iframe.contentWindow) {
  41              return '';
  42          }
  43  
  44          var prefix = HistoryHash.hashPrefix,
  45              hash   = iframe.contentWindow.location.hash.substr(1);
  46  
  47          return prefix && hash.indexOf(prefix) === 0 ?
  48                      hash.replace(prefix, '') : hash;
  49      };
  50  
  51      /**
  52       * Updates the history iframe with the specified hash.
  53       *
  54       * @method _updateIframe
  55       * @param {String} hash location hash
  56       * @param {Boolean} replace (optional) if <code>true</code>, the current
  57       *   history state will be replaced without adding a new history entry
  58       * @protected
  59       * @static
  60       * @for HistoryHash
  61       */
  62      HistoryHash._updateIframe = function (hash, replace) {
  63          var iframeDoc      = iframe && iframe.contentWindow && iframe.contentWindow.document,
  64              iframeLocation = iframeDoc && iframeDoc.location;
  65  
  66          if (!iframeDoc || !iframeLocation) {
  67              return;
  68          }
  69  
  70          Y.log('updating history iframe: ' + hash + ', replace: ' + !!replace, 'info', 'history');
  71  
  72          if (replace) {
  73              iframeLocation.replace(hash.charAt(0) === '#' ? hash : '#' + hash);
  74          } else {
  75              iframeDoc.open().close();
  76              iframeLocation.hash = hash;
  77          }
  78      };
  79  
  80      Do.before(HistoryHash._updateIframe, HistoryHash, 'replaceHash', HistoryHash, true);
  81  
  82      if (!iframe) {
  83          Y.on('domready', function () {
  84              var lastUrlHash = HistoryHash.getHash();
  85  
  86              // Create a hidden iframe to store history state, following the
  87              // iframe-hiding recommendations from
  88              // http://www.paciellogroup.com/blog/?p=604.
  89              //
  90              // This iframe will allow history navigation within the current page
  91              // context. After navigating to another page, all but the most
  92              // recent history state will be lost.
  93              //
  94              // Earlier versions of the YUI History Utility attempted to work
  95              // around this limitation by having the iframe load a static
  96              // resource. This workaround was extremely fragile and tended to
  97              // break frequently (and silently) since it was entirely dependent
  98              // on IE's inconsistent handling of iframe history.
  99              //
 100              // Since this workaround didn't work much of the time anyway and
 101              // added significant complexity, it has been removed, and IE6 and 7
 102              // now get slightly degraded history support.
 103              Y.log('creating dynamic history iframe', 'info', 'history');
 104  
 105              iframe = GlobalEnv._iframe = Y.Node.getDOMNode(Y.Node.create(
 106                  '<iframe src="javascript:0" style="display:none" height="0" width="0" tabindex="-1" title="empty"/>'
 107              ));
 108  
 109              // Append the iframe to the documentElement rather than the body.
 110              // Keeping it outside the body prevents scrolling on the initial
 111              // page load (hat tip to Ben Alman and jQuery BBQ for this
 112              // technique).
 113              Y.config.doc.documentElement.appendChild(iframe);
 114  
 115              // Update the iframe with the initial location hash, if any. This
 116              // will create an initial history entry that the user can return to
 117              // after the state has changed.
 118              HistoryHash._updateIframe(lastUrlHash || '#');
 119  
 120              // Listen for hashchange events and keep the iframe's hash in sync
 121              // with the parent frame's hash.
 122              Y.on('hashchange', function (e) {
 123                  lastUrlHash = e.newHash;
 124  
 125                  if (HistoryHash.getIframeHash() !== lastUrlHash) {
 126                      Y.log('updating iframe hash to match URL hash', 'info', 'history');
 127                      HistoryHash._updateIframe(lastUrlHash);
 128                  }
 129              }, win);
 130  
 131              // Watch the iframe hash in order to detect back/forward navigation.
 132              Y.later(50, null, function () {
 133                  var iframeHash = HistoryHash.getIframeHash();
 134  
 135                  if (iframeHash !== lastUrlHash) {
 136                      Y.log('updating URL hash to match iframe hash', 'info', 'history');
 137                      HistoryHash.setHash(iframeHash);
 138                  }
 139              }, null, true);
 140          });
 141      }
 142  }
 143  
 144  
 145  }, '3.17.2', {"requires": ["history-hash", "node-base"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1