max
max

Reputation: 39

Maintain state of page after refresh

I'm tryiing to save the state of a page i load into a div onclick function. When i click on the link the page is loaded into the div correctly but when i refresh the page i lose the content and will have to click again to load

<script language="javascript">


$(document).ready(function () {
    $('#foo').click(function () {
        $('#bar').load('news.asp');

        localStorage.setItem('display', $(this).siblings().is(':visible'));
    });
    var block = localStorage.getItem('display');
    if (block == 'true') {
        $('#bar').show()
    }
});

</script>

html

<div id="bar"></div>

Upvotes: 0

Views: 2789

Answers (1)

George
George

Reputation: 36784

You need to store the content in localStorage, not the result of .is(':visible').

You also need to append the content to #bar when the page refreshes:

$(document).ready(function () {
    $bar = $('#bar');
    $('#foo').click(function () {
        $bar.load('news.asp');
        localStorage.setItem('display', $bar.html());
    });

    var block = localStorage.getItem('display');
    if (block !== null) {
        $bar.html(block).show()
    }
});

Upvotes: 1

Related Questions