如何在javascript变量中永久存储值

时间:2021-01-02 23:07:27

I want to make the application like below.

我想制作如下的应用程序。

First there is a page with four radio buttons. The user clicks on any of the radio buttons but doesn't save and he goes to another page. Then if he comes back to same page with the radio buttons it shows the selected radio button before he goes to another page.

首先是一个带有四个单选按钮的页面。用户点击任何单选按钮但不保存,他转到另一个页面。然后,如果他带着单选按钮回到同一页面,它会在进入另一页面之前显示所选的单选按钮。

How can I do it by using jQuery, jsp, ajax and javascript ?

我怎么能通过使用jQuery,jsp,ajax和javascript来做到这一点?

I can't use any session or coockies and database access. here I am redirecting the page by "location.href"

我不能使用任何会话或coockies和数据库访问。这里我通过“location.href”重定向页面

Please, give me the solution as soon as possible

请尽快给我解决方案

3 个解决方案

#1


2  

You can use YUI 2: Storage Utility:

您可以使用YUI 2:存储实用程序:

The Storage Utility provides a mechanism for storing significant amounts of textual data, client-side, whether or not your browsers supports the proposed HTML 5 Storage specification.

无论您的浏览器是否支持建议的HTML 5存储规范,存储实用程序都提供了一种存储大量文本数据的机制,客户端。

Each instance of the Storage Utility leverages one of three storage engines in order to store data:

存储实用程序的每个实例都利用三个存储引擎之一来存储数据:

  • HTML 5: If the client browser supports HTML 5, then this engine will wrap the browser's native storage capability (document.localStorage and document.sessionStorage).
  • HTML 5:如果客户端浏览器支持HTML 5,则此引擎将包装浏览器的本机存储功能(document.localStorage和document.sessionStorage)。

  • Google Gears: Google Gears is a browser extension that users can install on their machine. One of its features is a SQLite database; the Storage Utility uses this database for client-side storage when using the Gears engine.
  • Google Gears:Google Gears是一种浏览器扩展程序,用户可以在其计算机上安装。它的一个功能是SQLite数据库;使用Gears引擎时,Storage Utility将此数据库用于客户端存储。

  • SWF: YUI provides a SWFStore Utility that normalizes access to the Flash Shared Object. This is the Storage Utility's fallback engine, which will work on most browsers due to the significant penetration of the Adobe Flash plugin.
  • SWF:YUI提供了一个SWFStore实用程序,可以规范化对Flash共享对象的访问。这是Storage Utility的后备引擎,由于Adobe Flash插件的大量渗透,它可以在大多数浏览器上运行。


General Usage Pattern

The following is the general code pattern used to fetch an engine, then write/read some data:

以下是用于获取引擎,然后写入/读取一些数据的通用代码模式:

// this will fetch the first available engine
var storageEngine = YAHOO.util.StorageManager.get();

storageEngine.subscribe(storageEngine.CE_READY, function() {
    storageEngine.setItem('testText', 'this is a triumph');
    storageEngine.setItem('testNumber', 1234567890);
    storageEngine.setItem('testBoolean', true);
    alert(storageEngine.getItem('testText'));
});

Of course, the simplest and most low tech approach would be to use a cookie, for which there is a jQuery Plugin.

当然,最简单和最低技术的方法是使用cookie,其中有一个jQuery插件。

It's basic usage is like this:

它的基本用法是这样的:

<script src="../jquery-1.3.min.js" type="text/javascript"></script> 
<script src="jquery.cookie.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(function() {
        var COOKIE_NAME = 'test_cookie';
        var ADDITIONAL_COOKIE_NAME = 'additional';
        var options = { path: '/', expires: 10 };

        // set cookie by number of days
        $('a').eq(0).click(function() {
            $.cookie(COOKIE_NAME, 'test', options);
            return false;
        });

        // set cookie by date
        $('a').eq(1).click(function() {
            var date = new Date();
            date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
            $.cookie(COOKIE_NAME, 'test', { path: '/', expires: date });
            return false;
        });

        // get cookie
        $('a').eq(2).click(function() {
            alert($.cookie(COOKIE_NAME));
            return false;
        });

        // delete cookie
        $('a').eq(3).click(function() { 
            $.cookie(COOKIE_NAME, null, options);
            return false;
        });
</script> 

#2


0  

Sounds like you want to store the value in a cookie.

听起来你想将值存储在cookie中。

EDIT: Here's a jQuery plugin for working with cookies: http://plugins.jquery.com/project/Cookie

编辑:这是一个用于处理cookie的jQuery插件:http://plugins.jquery.com/project/Cookie

#3


0  

Either append the selection to all links as a GET parameter (probably NOT the way to go but one way to let the client handle the state) or use cookies (preferably the jQuery cookie API which is a little easier than vanilla JavaScript cookie handling).

将选择作为GET参数附加到所有链接(可能不是一种让客户端处理状态的方法)或使用cookie(最好是jQuery cookie API,它比vanilla JavaScript cookie处理容易一些)。

#1


2  

You can use YUI 2: Storage Utility:

您可以使用YUI 2:存储实用程序:

The Storage Utility provides a mechanism for storing significant amounts of textual data, client-side, whether or not your browsers supports the proposed HTML 5 Storage specification.

无论您的浏览器是否支持建议的HTML 5存储规范,存储实用程序都提供了一种存储大量文本数据的机制,客户端。

Each instance of the Storage Utility leverages one of three storage engines in order to store data:

存储实用程序的每个实例都利用三个存储引擎之一来存储数据:

  • HTML 5: If the client browser supports HTML 5, then this engine will wrap the browser's native storage capability (document.localStorage and document.sessionStorage).
  • HTML 5:如果客户端浏览器支持HTML 5,则此引擎将包装浏览器的本机存储功能(document.localStorage和document.sessionStorage)。

  • Google Gears: Google Gears is a browser extension that users can install on their machine. One of its features is a SQLite database; the Storage Utility uses this database for client-side storage when using the Gears engine.
  • Google Gears:Google Gears是一种浏览器扩展程序,用户可以在其计算机上安装。它的一个功能是SQLite数据库;使用Gears引擎时,Storage Utility将此数据库用于客户端存储。

  • SWF: YUI provides a SWFStore Utility that normalizes access to the Flash Shared Object. This is the Storage Utility's fallback engine, which will work on most browsers due to the significant penetration of the Adobe Flash plugin.
  • SWF:YUI提供了一个SWFStore实用程序,可以规范化对Flash共享对象的访问。这是Storage Utility的后备引擎,由于Adobe Flash插件的大量渗透,它可以在大多数浏览器上运行。


General Usage Pattern

The following is the general code pattern used to fetch an engine, then write/read some data:

以下是用于获取引擎,然后写入/读取一些数据的通用代码模式:

// this will fetch the first available engine
var storageEngine = YAHOO.util.StorageManager.get();

storageEngine.subscribe(storageEngine.CE_READY, function() {
    storageEngine.setItem('testText', 'this is a triumph');
    storageEngine.setItem('testNumber', 1234567890);
    storageEngine.setItem('testBoolean', true);
    alert(storageEngine.getItem('testText'));
});

Of course, the simplest and most low tech approach would be to use a cookie, for which there is a jQuery Plugin.

当然,最简单和最低技术的方法是使用cookie,其中有一个jQuery插件。

It's basic usage is like this:

它的基本用法是这样的:

<script src="../jquery-1.3.min.js" type="text/javascript"></script> 
<script src="jquery.cookie.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(function() {
        var COOKIE_NAME = 'test_cookie';
        var ADDITIONAL_COOKIE_NAME = 'additional';
        var options = { path: '/', expires: 10 };

        // set cookie by number of days
        $('a').eq(0).click(function() {
            $.cookie(COOKIE_NAME, 'test', options);
            return false;
        });

        // set cookie by date
        $('a').eq(1).click(function() {
            var date = new Date();
            date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
            $.cookie(COOKIE_NAME, 'test', { path: '/', expires: date });
            return false;
        });

        // get cookie
        $('a').eq(2).click(function() {
            alert($.cookie(COOKIE_NAME));
            return false;
        });

        // delete cookie
        $('a').eq(3).click(function() { 
            $.cookie(COOKIE_NAME, null, options);
            return false;
        });
</script> 

#2


0  

Sounds like you want to store the value in a cookie.

听起来你想将值存储在cookie中。

EDIT: Here's a jQuery plugin for working with cookies: http://plugins.jquery.com/project/Cookie

编辑:这是一个用于处理cookie的jQuery插件:http://plugins.jquery.com/project/Cookie

#3


0  

Either append the selection to all links as a GET parameter (probably NOT the way to go but one way to let the client handle the state) or use cookies (preferably the jQuery cookie API which is a little easier than vanilla JavaScript cookie handling).

将选择作为GET参数附加到所有链接(可能不是一种让客户端处理状态的方法)或使用cookie(最好是jQuery cookie API,它比vanilla JavaScript cookie处理容易一些)。