最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use "Session" in jQuery? - Stack Overflow

programmeradmin0浏览0评论

Is there a similar functionality in jQuery as Session["Param"] in C#? How can I use it?

I've searched "sessionStorage" in jQuery, but I can't understand.

Is there a similar functionality in jQuery as Session["Param"] in C#? How can I use it?

I've searched "sessionStorage" in jQuery, but I can't understand.

Share Improve this question edited Jul 27, 2016 at 17:56 TankorSmash 12.8k6 gold badges70 silver badges108 bronze badges asked Jul 27, 2016 at 17:47 Eric SaboiaEric Saboia 3212 gold badges10 silver badges22 bronze badges 1
  • No need for jQuery, developer.mozilla/en-US/docs/Web/API/Window/sessionStorage – Alexander O'Mara Commented Jul 27, 2016 at 17:49
Add a ment  | 

4 Answers 4

Reset to default 6

As you said, you can use sessionStorage in JavaScript.

▶ You can set a new parameter like so:

sessionStorage.param1 = "Hello";
/* Or */
sessionStorage.setItem("param1", "Hello");

▶ Then, you can get it as follows:

var param = sessionStorage.param1;
/* Or */
var param = sessionStorage.getItem("param1");

console.log(param); /* It'll output `Hello`. */

Notes:

▶ Using sessionStorage, you can only store data for one session, which means everything will be deleted if you close the specific browser tab. If you want to permanently store data use localStorage instead.

▶ As you are apparently very new to JavaScript, I suggest you take a look at the following documentation about Web Storage in JavaScript:

  • Mozilla Development Network (sessionStorage, localStorage)
  • W3 Schools (both)
// ${FEEDBACK_QUESTION_IDS} this is session attribute name in controller

<script type="text/javascript">
    $(document).ready(function() {  
        window.questionIdsList = [];
        var i = 0;
        <c:forEach items="${FEEDBACK_QUESTION_IDS}" var="queId">
        questionIdsList[i] = parseInt(${queId});
        i++;
        </c:forEach>

    });
</script>

Finally we can use window.questionIdsList as same as array

The following can be used to store and retrieve data in the following ways:

To store:

sessionStorage.setItem("error", 'some value');

To retrieve:

sessionStorage.getItem("error");

To use session data ,you don't need jquery you can use setItem ,getItem and removeItem to deal with the session data which is stored as a json object and accessible via key.

发布评论

评论列表(0)

  1. 暂无评论