devote / HTML5-History-API

HTML5 History API expansion for browsers not supporting pushState, replaceState
http://spb-piksel.ru
MIT License
1.02k stars 182 forks source link

history.location.href incorrect in IE9 #54

Closed jonagoldman closed 10 years ago

jonagoldman commented 10 years ago

Hi, In IE9, the history.location.href is incorrect, but document.location.href is correct, as you can see in the next image:

capture

This always happens whit the first page we browse to, so I think this is because the first page we enter is not saved correctly in the history state.

Any idea how to fix this? Thanks, and great script by the way!

njam commented 10 years ago

Probably this?

                    /*
                     * Note, this is the only difference when using this library,
                     * because the object document.location cannot be overriden,
                     * so library the returns generated "location" object within
                     * an object window.history, so get it out of "history.location".
                     * For browsers supporting "history.pushState" get generated
                     * object "location" with the usual "document.location".
                     */
                    var returnLocation = history.location || document.location;
devote commented 10 years ago

This is not a bug. This is correct operation history.location in non-HTML5 browsers

jonagoldman commented 10 years ago

So if I understand correctly the next line alone is not enough, and can something lead to undesired behavior:

 var returnLocation = history.location || document.location;

and I need to use Modernizr or similar to detect the real browser capabilities, something like:

var returnLocation = document.location;
if (Modernizr.history) {
    returnLocation = history.location;
}

or without Modernizr:

var returnLocation = document.location;
if (window.history && window.history.pushState) {
    returnLocation = history.location;
}

Right?

devote commented 10 years ago

Enough to use:

var returnLocation = history.location || document.location;

I write in their projects as follows:

(function() {
  var location = history.location || document.location;
  // ...
  // ... // other code
  // ...
  console.log(location.href);
  // ...
  // ... // other code
  // ...
})();
jonagoldman commented 10 years ago

But if I use it like that then in IE9 history.location.href is incorrect and different from other browsers, but I want the same behavior for all.

devote commented 10 years ago

I understand you. You want the impossible. But, IE9 has no support for HTML5-History-API, for this reason this library emulates this feature.

If your site is located in the root folder: /he/project/2/. To do this you need to add the directive basepath with a value /he/project/2/.

for example:

<script src="history.js?basepath=/he/project/2/"></script>

or

<script src="history.js"></script>
<script>
  history.redirect(null, '/he/project/2/');
</script>
jonagoldman commented 10 years ago

Oh, I will try that. Thanks!

devote commented 10 years ago

I close this issue, if you still want to solve this, let me know. Thank you