使用 html/javascript 在没有数据库的网页上存储数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33586351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store data on a web page without a database using html/javascript
提问by Jordando
I have an assignment in which I need to use just html and javascript to store some records (let's just say a name and a phone number), that can be deleted/updated later. The catch is I can not use a database of any kind.
我有一个作业,我只需要使用 html 和 javascript 来存储一些记录(让我们说一个名字和一个电话号码),以后可以删除/更新。问题是我不能使用任何类型的数据库。
For simplicity sake, let's just say I just have a webpage that has 2 fields and 1 button: a name field, a phone number field, and a submit button. When the user presses submit, the information needs to be stored.
为简单起见,假设我只有一个包含 2 个字段和 1 个按钮的网页:姓名字段、电话号码字段和提交按钮。当用户按下提交时,需要存储信息。
In my experience with Java, I would do something like this: get user input, save data to a .txt file. So I thought perhaps I can do something similar, however, in my search so far the responses I see make me believe that there is no way to create a .txt (or any type of) file using javasript/html.
根据我使用 Java 的经验,我会做这样的事情:获取用户输入,将数据保存到 .txt 文件。所以我想也许我可以做一些类似的事情,但是,到目前为止,在我的搜索中,我看到的回复让我相信没有办法使用 javasript/html 创建 .txt(或任何类型的)文件。
So my question is just this: how can I save data using just html/javascript? Or is this even possible?
所以我的问题就是:如何仅使用 html/javascript 保存数据?或者这甚至可能吗?
Thanks!
谢谢!
回答by Alberto
take a look: http://www.w3schools.com/HTML/html5_webstorage.asp
看看:http: //www.w3schools.com/HTML/html5_webstorage.asp
You can use web storage.
您可以使用网络存储。
From W3Schools:
来自 W3Schools:
With web storage, web applications can store data locally within the user's browser.
Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.
Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.
Web storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.
通过网络存储,网络应用程序可以在用户浏览器中本地存储数据。
在 HTML5 之前,应用程序数据必须存储在 cookie 中,包含在每个服务器请求中。Web存储更安全,可以在本地存储大量数据,不影响网站性能。
与 cookie 不同的是,存储限制要大得多(至少 5MB)并且信息永远不会传输到服务器。
Web 存储是每个源(每个域和协议)。来自一个源的所有页面都可以存储和访问相同的数据。
回答by Zak
You have a few options here:
您在这里有几个选择:
The simplest way is HTML5 local storage. This is quite basic and allows you to set a name and value for each record. Take a look at https://developer.mozilla.org/en/docs/Web/API/Window/localStorage.
You can use either IndexedDB (https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) or Web SQL (http://www.tutorialspoint.com/html5/html5_web_sql.htm). Both of these are pretty similar in the sense that they both essentially create an offline database. However, some browsers support one and some support the other so it can be difficult to implement.
My personal favourite solution is YDN-DB (http://dev.yathit.com/ydn-db/index.html). It is a JavaScript library that uses all of the above methods to create a cross-browser compatible offline database. It can also sync with an online database as an added feature.
最简单的方法是 HTML5 本地存储。这是非常基本的,允许您为每条记录设置名称和值。看看https://developer.mozilla.org/en/docs/Web/API/Window/localStorage。
您可以使用 IndexedDB ( https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) 或 Web SQL ( http://www.tutorialspoint.com/html5/html5_web_sql.htm)。这两者在本质上都创建了一个离线数据库的意义上非常相似。但是,有些浏览器支持一种,有些则支持另一种,因此可能难以实现。
我个人最喜欢的解决方案是 YDN-DB ( http://dev.yathit.com/ydn-db/index.html)。它是一个 JavaScript 库,它使用上述所有方法来创建跨浏览器兼容的离线数据库。它还可以作为附加功能与在线数据库同步。
Hope this helps, feel free to leave a comment if you want a bit more info.
希望这会有所帮助,如果您想要更多信息,请随时发表评论。
回答by Rens Tillmann
Use cookies there is a plugin for jquery cookies, really handy :) you could try that maybe?
使用 cookie 有一个用于 jquery cookie 的插件,真的很方便 :) 你可以试试吗?
Or just install mysql with php and store it, or only php and write a json file, or txt files :)
或者只是用php安装mysql并存储它,或者只用php写一个json文件,或者txt文件:)
But I wonder why you don't have php ability, even if you work localhost you could easily setup xampp with apache mysql and php for example.
但是我想知道为什么您没有 php 能力,即使您在本地主机上工作,您也可以使用 apache mysql 和 php 轻松设置 xampp。