How to create onpopstate event on click back and forward button

I have created a jquery ajax code to display a content using pushstate, it works good but when I click on back and forward button the content doesn't change again. How to create event onpopstate in this case ?

    <script>
    $(document).ready(function () {

        $(".load-ajax").click(function (e) {
            e.preventDefault();
            var route = $(this).attr('href');

            $.ajax({
                type: 'get',
                dataType: 'json',
                url: Routing.generate('admin_order_state_new'),
                success: function (msg) {
                    if (msg["ok"] === undefined) {
                        alert('error');
                    } else {
                        window.history.pushState(null, "Title", route);

                        $("#mydiv").html(msg["ok"]);
                    }
                }
            });
            return false;
        });

    });

</script>

Answers


Solved

$(document).ready(function () {

function loadPage(route) {
    $.ajax({
        type: 'get',
        dataType: 'html',
        url: route,
        success: function (msg) {
            if (msg === undefined) {
                alert('error');
            } else {
                $('#content').replaceWith(
                    // with the returned one from the AJAX response.
                    $(msg).find('#content')
                );

            }
        }
    });
}

$(window).on('popstate', function () {
    loadPage(location.pathname);
});

$(".load-ajax").click(function (e) {
    e.preventDefault();
    var route = $(this).attr('href');

    window.history.pushState(null, "Title", route);

    loadPage(route)

 });


});

Need Your Help

Strange behavior x,y coordinates when moving UIImageView after rotating and scaling

ios objective-c uiimageview rotation cgaffinetransform

I've got a UIImageView added to a UIScrollView. You can tap the scrollview and another UIImageView is added to the "main" UIImageView. I use this code to get the coordinates from the touch and conv...

Eclipse Formatting. Remove new line before opening brace for block

java eclipse block braces

Is there a way to prevent Eclipse from adding a new line before block opening brace ?