LocationHash does not update in Chrome.

I am trying to keep the page state in a hash. This works in IE and FF, but in Chrome it does nothing

$(document).ready(function()
{
    window.onbeforeunload = savePageState;
});

function savePageState()
{
    var currentTab = _tabbing.getCurrentTab();

    var mapState = _mapAdapter.getMapState();
    window.location.hash =
        'reload=' + currentTab +
        '&mapType=' + mapState.MapType.getName() +
        '&lat=' + mapState.Latitude +
        '&long=' + mapState.Longitude +
        '&zoom=' + mapState.ZoomLevel;

    return;
}

Is there a lesson in Chrome that keeps me from updating the hash? Is there a better way to keep page state? Its only for the back button that you want to save.

+3
source share
2 answers

While the barbecue was good, this did not completely solve my problem. For it to work in Chrome / Safari, you need to update the hash before the onbeforeunload event. However, part of the requirement made it so that it was the only real moment when I could save the state of the page.

, , pushState BBQ. , , - , .

function BackButtonState(saveStateCallback, pageLoadCallback)
{
    var executePopStateCounter = null;
    var myData = {};
    var browserCanPushState = history.pushState ? true : false;

    window.onbeforeunload = saveStateCallback;

    if (browserCanPushState)
    {
        //The version of safari this was tested on (5.0.3) uses an outdated version of
        //Webkit that has a bug where popstate is not called on the first page load.
        //This is a hacky work around until the problem is fixed.
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("safari") != -1)
        {
            executePopStateCounter = setTimeout(pageLoadCallback, 500);
        };

        window.onpopstate = function(popEvent)
        {
            clearTimeout(executePopStateCounter);
            if (popEvent.state != null)
            {
                myData = popEvent.state;
            }
            pageLoadCallback();
        }
    }
    else
    {
        $(document).ready(pageLoadCallback);
    }

    this.savePageState = function(state)
    {
        if (browserCanPushState)
        {
            history.pushState(state, 'BackButtonState');
        }
        else
        {
            $.bbq.pushState(state);
        }
    }

    this.getState = function(item)
    {
        if (browserCanPushState)
        {
            return myData[item];
        }
        else
        {
            return $.bbq.getState(item);
        }
    }

    this.deserializeSortList = function(sortArrays)
    {
        var sortList = [];

        $.each(sortArrays, function(index, pair)
        {
            sortList.push([parseInt(pair[0]), parseInt(pair[1])]);
        });

        return sortList;
    }
}

, - :

function saveState()
{
    var myData = { bananas: 'are tasty' };
    _backButton.saveState(myData);

}

function pageSetup()
{
    //Do normal $(document).ready() stuff here
    var myOpinionOnFruit = _backButton.getState('bananas');
}

var _backButton = new BackButtonState(saveState, pageSetup);

, , , Safari. , .

+1

jQuery BBQ, .

$.bbq.pushState({
    reload: currentTab,
    mapType: mapState.MapType.getName(),
    lat: mapState.Latitude,
    long: mapState.Longitude,
    zoom: mapState.ZoomLevel
});

BBQ , . , #fun=yes #fun=yes&reload=tab&...

:

$(window).bind('hashchange', function(e) {
    var params = e.getState();
    doStuff(params.lat, params.long);
});
+3

Source: https://habr.com/ru/post/1787687/


All Articles