Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:47:23+00:00 2026-06-18T14:47:23+00:00

I have some problem with the stateless nature of .NET MVC framework. I have

  • 0

I have some problem with the stateless nature of .NET MVC framework.

I have some HTML like

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
</ul>
<button id="btnShowMore">Show More</button>
<a href="www.google.com">Google</a>

and my javascript goes like

var maxDisplay = 5;
var unit = 5;
$("li").slice(maxDisplay).hide();
if ($("li").length <= maxDisplay) $('#btnShowMore').hide();
$('#btnShowMore').click(function () {
    maxDisplay += unit;
    $("li").slice(0, maxDisplay - 1).show();
    if ($("li").length <= maxDisplay) $('#btnShowMore').hide();
});

Now if I expand my list, navigate to google, and click back button in my browser, the list would forget what I did and shows only the first 5 list item, which is not what I want.

I thought of some options:

  1. cookie. not quite what I want because there are lots of pages like this, and I would need cookie for each of them

  2. manipulate url a bit, like taking advantage of window.location.hash. also not good, because that would be another page

Is there some conventional way to do this?

Thanks in advance


UPDATE:

seems like no more solutions coming out. I’d definitely prefer localStorage over cookie as there’s no need to transfer this variable between client and server, but there needs a check whether the page is first load (clear cached variable and show 5 items) or load from navigate back(read cache and show cached number of items).

maybe I should go with hash…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:47:25+00:00Added an answer on June 18, 2026 at 2:47 pm

    You could always use localStorage to persist your application state. And have your script adapted so that it reads and writes to it directly instead.

    Here are a couple of functions I commonly use to read and write from the localStorage

    function setStorageItem(key, value) {
        var localStorage = window.localStorage;
        if (localStorage) {
            try {
                localStorage.setItem(key, window.JSON.stringify(value));
            } catch (e) {
            }
        }
        return !!localStorage;
    }
    
    function getStorageItem(key, defaultValue) {
        var localStorage = window.localStorage;
        if (localStorage) {
            var value = localStorage.getItem(key);
            if (value) {
                return $.parseJSON(value);
            }
        }
        return defaultValue || undefined;
    }
    

    Your code would be:

    var maxDisplay = getStorageItem('maxDisplay', 5),
        unit = 5,
        more = $('#btnShowMore');
    
    $("li").slice(maxDisplay).hide();
    
    if ($("li").length <= maxDisplay){
        more.hide();
    }
    
    more.click(function () {
        maxDisplay += unit;
    
        setStorageItem('maxDisplay', maxDisplay); // remember it.
    
        $("li").slice(0, maxDisplay - 1).show();
    
        if ($("li").length <= maxDisplay){
            more.hide();
        }
    });
    

    If localStorage isn’t working, your code will work the same, and if it is, it’ll remember the maxDisplay property.

    Update

    In case you insist on using cookies:

    function setCookie(key, value, expires) {
        var defaults = {
            path: '/',
            expires: 1000 * 60 * 60 * 6 * 24 * 30 // 6 months
        };
        var date = new Date();
        var offset = expires || defaults.expires;
        date.setTime(date.getTime() + offset);
        var json = window.JSON.stringify(value);
        var cookie = '{0}={1}; expires={2}; path={3}'.format(key, json, date.toGMTString(), defaults.path);
        document.cookie = cookie;
    }
    
    function getCookie(key, defaultValue) {
        var identifier = '{0}='.format(key);
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i];
            while (cookie.charAt(0) == ' ') {
                cookie = cookie.substring(1, cookie.length);
            }
            if (cookie.indexOf(identifier) == 0) {
                var value = cookie.substring(identifier.length, cookie.length);
                return $.parseJSON(value);
            }
        }
        return defaultValue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem in understanding how does the asp.net mvc deal with Null
I have some problem - I must know when the button isn't clicked anymore.
I have some problem, I use python. I have 2 var, like ads =
I have some problem to get the utf-8 string from PHP using jQuery $.load()
I have some problem with the cursor when using JOprionPane. I set a cursor
I have some problem. Dialog.dismiss() does not work. I want to input ip, username,
I have some problem with this mongoid. It's my first time to use mongoDB,
I have some problem with MySQL Workbench in that I sometimes can't set foreign
I have some problem in my JDBC code. I am trying to connect through
I have some problem with Emit mapper when I try to save in database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.