简体   繁体   English

奇怪的sessionStorage行为

[英]Weird sessionStorage behavior

OK, bear with me please, I'll try to explain the issue as detailed as possible. 好的,请耐心等待,我会尽量详细解释这个问题。

I have 3 HTML (and appropriate .js) files. 我有3个HTML(和适当的.js)文件。 I can add stuff to sessionStorage and I display the content of the sessionStorage when I'm located in either of the HTML files. 我可以向sessionStorage添加内容,当我位于任一HTML文件中时,我会显示sessionStorage的内容。 I'm running files locally on the latest version of firefox, which - as far as I know - supports sessionStorage in file:// So, everything works great, I can add stuff to sessionStorage in HTML1 and go to HTML2 and the data is displayed properly. 我在最新版本的firefox上本地运行文件,据我所知 - 在文件中支持sessionStorage://所以,一切都很好,我可以在HTML1中添加东西到sessionStorage并转到HTML2,数据是显示得当。 I get the concept of sessionStorage and all is good. 我得到了sessionStorage的概念,一切都很好。

But here's where it gets weird: when I add stuff to sessionStorage in HTML1 and go to HTML2 via a-href, the content of sessionStorage is there. 但是这里变得奇怪:当我在HTML1中向sessionStorage添加内容并通过a-href转到HTML2时,sessionStorage的内容就在那里。 But if I click in the url bar and press enter, the content is lost. 但是,如果我在网址栏中单击并按Enter键,则内容将丢失。 SessionStorage is "null". SessionStorage为“null”。 I go back to HTML1 and press ctrl+r (reload) and still, null. 我回到HTML1并按ctrl + r(重新加载)仍然是null。 But when I click in url bar and press enter (while I'm in HTML1 page), sessionStorage content returns. 但是当我点击url栏然后按回车键(当我在HTML1页面时),sessionStorage内容返回。

I've done several checks and it appears as if when I click in url bar and press enter, another sessionStorage instance is created. 我已经完成了几次检查,看起来好像当我点击url栏并按回车键时,会创建另一个sessionStorage实例。

This, however, does NOT happen if I upload the project on some server. 但是,如果我在某个服务器上传项目,则不会发生这种情况。 I'm wondering why - if firefox supports sessionStorage - this is happening when I run the application locally and what is really going on there? 我想知道为什么 - 如果firefox支持sessionStorage - 当我在本地运行应用程序并且那里真正发生了什么时,会发生这种情况? Thanks for any kind of explanation. 谢谢你的任何解释。

I'm quite sure that if you looks at files from the filesystem, browsers consider every single file like a domain. 我很确定如果您查看文件系统中的文件,浏览器会将每个文件视为域。 So no Ajax, no shared cookies, sessions etc. The thing I find weird is that you get to keep some sessions... 所以没有Ajax,没有共享cookie,会话等等。我觉得奇怪的是你要保留一些会话......

Imagine you have a user stupid enough to open an html file he downloaded, without those protections, that html file could see everything on the system. 想象一下,你有一个愚蠢的用户打开他下载的html文件,没有这些保护,html文件可以看到系统上的所有内容。 And as much as antiviruses and the OS warn you when you open a .exe (or equivalent), you won't get any kind of warning when opening a .html file. 当打开.exe(或等效文件)时,防病毒软件和操作系统会警告您,打开.html文件时不会收到任何警告。

You'll have to use a local server, something like WAMP probably. 你必须使用本地服务器,比如WAMP。 Or Apache if you can. 或者如果可以的话就是Apache。

Ok, it doesn't work locally since you are opening the files, you need to run it on a server. 好的,它在本地不起作用,因为你打开文件,你需要在服务器上运行它。

The browser will tie the sessions together using the URL (and other internal stuff of course) which is why it works on the whatever server you upload to. 浏览器会使用URL(当然还有其他内部内容)将会话绑定在一起,这就是为什么它适用于您上传到的任何服务器。 This won't work with the file URLs as the browser seems them as different pages from different locations, and so the session isn't shared. 这不适用于文件URL,因为浏览器将它们视为来自不同位置的不同页面,因此不会共享会话。

If you set up WAMP or something locally and run it through that, it will work. 如果您在本地设置WAMP或其他东西并通过它运行它,它将工作。 Which is exactly what xavierm02 suggested. 这正是xavierm02建议的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM