browserstate / history.js

History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports jQuery, MooTools and Prototype. For HTML5 browsers this means that you can modify the URL directly, without needing to use hashes anymore. For HTML4 browsers it will revert back to using the old onhashchange functionality.
http://browserstate.github.com/history.js/demo/
Other
10.74k stars 1.35k forks source link

History pushstate in ie adding duplicate hash data #404

Open samrat-more opened 10 years ago

samrat-more commented 10 years ago

History.pushState appending extra #data in URL.

Browser URL other than Internet Explorer

http://localhost/shop/contracts-and-deals/mobile-phones?&_dyncharset=ISO-8859-1&_dynSessConf=8052455241245685514&planItemSkuId=&device=phones&contractType=onAccount&_D%3AcontractType=+&manufactureFilter=800005&_D%3AmanufactureFilter=+&_DARGS=%2Fshop%2FmobilePhones%2Fphones%2Ffilters%2FphonesLeftPanel.jsp&_dyncharset=ISO-8859-1&_dynSessConf=8052455241245685514&_D%3AsortFilter=+&sortFilter=most_popular&sort-view=grid&_DARGS=%2Fshop%2FmobilePhones%2Fphones%2Ffilters%2FdeviceTopFilter.jsp

Internet Explorer URL

http://localhost/shop/contracts-and-deals/mobile-phones#mobile-phones?&_dyncharset=ISO-8859-1&_dynSessConf=8052455241245685514&planItemSkuId=&device=phones&contractType=onAccount&_D%3AcontractType=+&manufactureFilter=800005&_D%3AmanufactureFilter=+&_DARGS=/shop/mobilePhones/phones/filters/phonesLeftPanel.jsp&_dyncharset=ISO-8859-1&_dynSessConf=8052455241245685514&_D%3AsortFilter=+&sortFilter=most_popular&sort-view=grid&_DARGS=/shop/mobilePhones/phones/filters/deviceTopFilter.jsp

Extra #mobile-phones getting appended after using History.pushState in Internet Explorer.

I Have tried below code.

var url = window.location.pathname;
var urlparts = url.split('/');
var currentState = urlparts[urlparts.length-1];

if(f && f != 'undefined' && f.hasClass('filterParams')) {
    var atgPrefix = "_D%3A";
    var totalParams = b.data;
    var arrayOfParams = totalParams.split("&");
    var paramSize = arrayOfParams.length;
        for(var i = 0; i < paramSize;i++){
            if(arrayOfParams[i].indexOf(atgPrefix) != -1) {
                var inputField = arrayOfParams[i].substring(arrayOfParams[i].indexOf(atgPrefix)+atgPrefix.length).split("=")[0];
                if(totalParams.indexOf("&"+inputField) == -1) {
                    totalParams = totalParams.replace(arrayOfParams[i]+"&",'');
                } else {
                    var atgHiddenFields = totalParams.match(new RegExp(arrayOfParams[i], 'g'));
                    var size = atgHiddenFields.length-1; 
                    for(var j = 0;j < size;j++) {
                        totalParams = totalParams.replace(arrayOfParams[i]+"&",'');
                    }
                }
            }
        }
        b.data = totalParams;
}
if(!($(a).closest('form').is('#colorPicker'))){
    if ( document.location.protocol === 'file:' ) {
        alert('The HTML5 History API (and thus History.js) do not work on files, please upload it to a server.');
    }
    var History = window.History, // Note: We are using a capital H instead of a lower h
    State = History.getState(),
    $log = $('#log');

    // Log Initial State
    History.log('initial:', State.data, State.title, State.url);

    // Bind to State Change
    History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
        // Log the State
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        History.log('statechange:', State.data, State.title, State.url);

    });
    History.pushState(b.data, document.title, currentState+"?"+b.data);
}

I have raised this issue in stackoverflow as well. Link: http://stackoverflow.com/questions/24387055/history-pushstate-in-ie9-adding-hash-tag

eberon commented 10 years ago

Same issue here. I get the page name as the first thing in the fragment identifier. It prevents me from (trivially) using the hash as a querystring to something else.

I feel bad for the person who answered you on SO who clearly went to a lot of work to explain this to someone who is using this library already (and understands the concept of history state) when he could have just read your two URLs and noticed the difference between them, what he said, and the examples on the demo site. For some reason, the demo site doesn't have the same behavior for me in IE9.

eberon commented 10 years ago

After looking at the demo again it occurs to me that the demo URL ends in /.

I moved my script into a directory and named it index -- voila. The hash state no longer includes the file name. Seems like a huge oversight to me? I can't see why you would need the filename as part of the hash. If nothing else it's definitely undocumented behavior.