HTML5 History API - 状态对象的最大大小是多少?

问题描述 投票:0回答:5

pushState
方法接受一个状态对象。 Firefox 文档称该对象的最大大小为 640kb。规范中是否定义了浏览器可以实现的最小最大尺寸是多少?我可以合理地期望主要浏览器为我提供至少 100kb 的空间吗?

编辑:我用 Chrome 对其进行了测试,它仍然适用于超过 1MB 的状态对象。

javascript html pushstate
5个回答
18
投票

规范没有规定限制,但是各种浏览器确实有自己的限制。

Firefox 有详细的文档记录,正如您所说,它有 640kB(“任何人都需要的 RAM”)。

我找不到任何地方列出的 Chrome 或 Internet Explorer,但一些快速测试显示:

Chrome 工作空间至少可达 10MB(甚至可能更多),

IE 达到了 1MB 的限制(在 IE11 中,这是我手头上的全部)。

所以,为未来的人们总结一下: History.state 对象大小限制为: Firefox 为

640kB
,Internet Explorer 11 为
1MB
,Chrome 至少为 10Mb

编辑:测试版本:IE:11、Chrome:33、Firefox:无关,因为它们在 MDN 上为您记录了最大大小:)。


9
投票
http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#dom-history-pushstate

,它甚至没有提到数据的限制尺寸。但建议采用不同的限制:

用户代理可能会限制数量 添加到会话的状态对象 每页历史记录。

正如您在此示例中看到的,规范通常避免提及任何硬性限制,并将其留给浏览器制造商自行决定。因此,即使将来某个时候修改规范以考虑数据大小限制的可能性,它也不太可能为您提供真实的数字。相反,它将“对于常见用例来说足够大”。


0
投票
https://developer.mozilla.org/en-US/docs/DOM/Manipulated_the_browser_history


0
投票
我通过以下代码处理了这个问题:


function pushState(data, title, url) { if (data.length > 524282) { //can't push the data to the History API--pass null history.pushState(null, title, url); history.replaceState(null, title, url); } else { history.pushState(data, title, url); history.replaceState(data, title, url); } document.title = title; }

在通过 ajax 请求加载新内容之前,我调用 beforeNavigate 来保存用户所做的任何当前位置信息或状态更改。

function beforeNavigate(){ if ($("#container").html().length <= 524282) { //save current state to history before navigating via ajax history.replaceState($("#container").html(), document.title, window.location.pathname); } }

通过监听 popstate 来处理后退和前进按钮。如果我们传递一个 null 值给 data,那么 e.state 将返回 null,我们需要通过 ajax 请求加载存储的 url。

window.addEventListener('popstate', function (e) { if (e.state!=null) { $("#container").html(e.state); } else { //load the partialpage into the container(a full html page is not returned by the ajax query for my site) $.ajax({ url: location.href, type: "GET", success: function (data) { $('#container').html(data); } }); } });



0
投票
history.state

施加 16MiB 的限制:

https://developer.mozilla.org/en-US/docs/Web/API/History/pushState#parameters
Chrome 和其他浏览器仍然没有记录这一点,请参阅其他答案以了解详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.