Frage

I have existing URLs that used the URL hash to provide extra data to javascript after page load, e.g. http://www.example.com/my/directory/#/foo/bar/

I'm implementing jquery.history for something else, but also need to make those old URLs continue to work (e.g. if someone might have bookmarked it with the /#... part).

However on page load the plugin is stripping out everything between the domain and the /#, e.g. the URL ends up becoming: http://www.example.com/foo/bar/

This isn't happening in IE 9, but is happening in Firefox and Chrome.

Any ideas? I don't consider this a bug in the plugin, but I'm happy to amend the unminified source files for my own usage, if someone suggests a fix that won't break the rest of its functionality.

One possible idea - I no longer care about what that extra data in the URL after the # is, so could perhaps remove that part from the URL before the history plugin does anything to it. I'm not entirely sure yet at what point that could be, or more specifically when the plugin is amending the URL on page load.

War es hilfreich?

Lösung

In the end I did what I mentioned at the end of my question. Added this bit of inline JS prior to the call to the history plugin js file:

(function(){
    if (document.location.hash.length !== 0) {
        document.location.hash = '';
    }
})();

At worst we end up with the URL still having a trailing # at the end, but as it doesn't have #/ then the history plugin doesn't then mess it up.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top