将 html 保存到 cookie 中

发布于 2024-11-07 09:37:28 字数 213 浏览 0 评论 0原文

我正在使用 jquery 克隆一些 html 并将其附加到 div 中。

我的问题是刷新页面时该 html 将被删除。问题是我如何将附加的 HTML 保存在 div 中(html 代码将包含一些输入和选择列表,并且会有大量代码)。

我应该检查什么?

Cookie?

HTML5?

还要别的吗?

I am using jquery to clone and append some html into a div.

My problem is that this html will be removed when the page is refreshed. The question is how I can save the appended HTML in the div (the html code will contain some inputs and select lists and there will be plenty of code).

What should I look into?

Cookies?

HTML5?

Anything else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疯到世界奔溃 2024-11-14 09:37:28

如果您只想支持现代浏览器,我会选择 HTML5 的离线存储。

另一种方法是获取 AJAX 路由。

您不想为此使用 cookie,因为:

每次请求时都会加载 cookie

cookie 只能保存有限数量的信息。

If you're only going to support modern browsers I would go for HTML5's offline storage.

Another approach would be to get the AJAX route.

You don't want to use cookies for this, bacause:

cookies get loaded at every request

cookies can only hold limited amount of information.

两仪 2024-11-14 09:37:28

使用众多 jquery 存储插件之一。其中许多使用html5存储,但如果浏览器目前不支持html5存储,则会使用其他技术。例如,使用 JQUERY STORAGE 插件,您可以通过以下方式保存数据:

$.Storage.set("name", "value");

并通过以下方式恢复数据:

$.Storage.get("name");

如果您要将代码附加到另一个元素,我建议您也保存要附加到存储的相同数据,而不是从文档中选择并获取它(速度要快一些)。

Use one of plenty jquery storage plugins. Many of them uses html5 storage, but if there is not present support for html5 storage in browser, it will use other techniques. For example with JQUERY STORAGE plugin you would save data in this way:

$.Storage.set("name", "value");

and restore it in this way:

$.Storage.get("name");

If you are appending code to another element, i recommend to save same data you are appending to storage too instead of selecting and getting it from document (it's a bit faster).

鸠书 2024-11-14 09:37:28

你可以尝试这个功能

<php
function getVar($name, $def = '') {
  if (isset($_REQUEST[$name]))
    return $_REQUEST[$name];
  else 
    return $def;
}


// check if overrides passed
$efind = getVar('q', '');
?>

这个输入标签的例子

<input type="text" name="q"  onFocus="this.value=''" onBlur="if(this.value == '')this.value ='<?php echo htmlentities($efind); ?>'" value="<?php echo htmlentities($efind); ?>" />

如果我犯了错误请纠正

you can try to this function

<php
function getVar($name, $def = '') {
  if (isset($_REQUEST[$name]))
    return $_REQUEST[$name];
  else 
    return $def;
}


// check if overrides passed
$efind = getVar('q', '');
?>

this example for input tag

<input type="text" name="q"  onFocus="this.value=''" onBlur="if(this.value == '')this.value ='<?php echo htmlentities($efind); ?>'" value="<?php echo htmlentities($efind); ?>" />

please correct if I make mistakes

毁我热情 2024-11-14 09:37:28

绝对不要尝试在 cookie 中存储 HTML...单独的大小限制 1 不会使其成为一个不错的选择。然而,您可能想在 cookie 中保存一个“标志”,当页面重新呈现时,触发 HTML 代码的重新插入(通过重新调用第一次添加它的函数,或者通过将其拉入)通过 AJAX。)

但是,我建议您在用户将内容附加到页面时对服务器进行 AJAX 回调。此调用将更新服务器上的对象/数据库,以便在重新加载页面时,它会带回完整内容,包括用户动态添加的部分。

RFC 2109

[1] RFC 2109 - section 6.3 (on HTTP Cookies)
    4096 bytes per cookie
    20 cookies per unique host or domain name

Definitely do not try to store HTML in cookies... the size limitation alone 1 would not make this a good option. However you might want to save a "flag" in the cookies that when the page re-renders, triggers the re-insertion of the HTML code (either by re-calling the function that added it the first time, or by pulling it in via AJAX.)

However what I would recommend is that you do an AJAX call back to the server when the user appends content to the page. This call would update the object/database on the server such that when the page is reloaded, it brings back the full content, including the part that was added dynamically by the user.

RFC 2109

[1] RFC 2109 - section 6.3 (on HTTP Cookies)
    4096 bytes per cookie
    20 cookies per unique host or domain name
紅太極 2024-11-14 09:37:28

Hakan,ajax 调用将使您的整页缓存变得无用(或至少不太有用),因为您的服务器将再次为每个请求加载 php+apache/nginx,因此您几乎不会获得任何性能,或者至少不会像完全全页缓存。我们计划做一个完整的缓存解决方案,第一次尝试时我们考虑对购物车使用 cookie 和 session,询问客户端是否支持 cookie,否则降级为 Ajax 调用。
告诉我你最终是否找到了解决方案。我计划使用 nginx 和 memcache 进行全页缓存,并使用 php + apc + apache + mod_sitespeed 进行后端请求

Hakan, the ajax call will do your full page cache useless (or at least less useful) cause your server will load the php+apache/nginx for every request again so you won't gain almost any performance, or at least not big as a completely full page cache. We are planing to do a full cache solution and at first attempt we thought in to use cookie and session for shopping cart, asking if the client supports cookie and downgrade to Ajax call otherwise.
Tell me if you finally got a solution. I'm planning to use nginx and memcache to full page cache and php + apc + apache + mod_sitespeed for backend requests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文