Javascript 跨子域使用 localStorage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4026479/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:11:08  来源:igfitidea点击:

use localStorage across subdomains

javascriptsubdomainlocal-storage

提问by JoJo

I'm replacing cookies with localStorageon browsers that can support it (anyone but IE). The problem is site.comand www.site.comstore their own separate localStorage objects. I believe www is considered a subdomain (a stupid decision if you ask me). If a user was originally on site.comand decides to type in www.site.comon her next visit, all her personal data will be inaccessible. How do I get all my "subdomains" to share the same localStorage as the main domain?

我在可以支持它的浏览器上用localStorage替换 cookie (除了 IE 之外的任何人)。问题是site.comwwwsite.com存储他们自己单独的 localStorage 对象。我相信 www 被认为是一个子域(如果你问我,这是一个愚蠢的决定)。如果用户最初在site.com并决定输入www。在她下次访问site.com 时,她的所有个人数据都将无法访问。如何让我的所有“子域”与主域共享相同的 localStorage?

采纳答案by Mayank Jain

This is how I use it across domains...

这就是我跨域使用它的方式...

  • Use an iframe from your parent domain - say parent.com
  • Then on each child.com domain, just do a postMessage to your parent.com iframe
  • All you need to do is setup a protocol of how to interpret your postMessage messages to talk to the parent.com iframe.
  • 使用来自您父域的 iframe - 比如 parent.com
  • 然后在每个 child.com 域上,只需向您的 parent.com iframe 发送 postMessage
  • 您需要做的就是设置一个协议,用于解释如何解释您的 postMessage 消息以与 parent.com iframe 对话。

I hope it helps :)

我希望它有帮助:)

回答by Matt

If you're using the iframe and postMessage solution just for this particular problem, I think it might be less work (both code-wise and computation-wise) to just store the data in a subdomain-less cookie and, if it's not already in localStorage on load, grab it from the cookie.

如果您仅针对这个特定问题使用 iframe 和 postMessage 解决方案,我认为将数据存储在无子域的 cookie 中可能会减少工作量(代码和计算),如果还没有在加载时的 localStorage 中,从 cookie 中获取它

Pros:

优点:

  • Doesn't need the extra iframe and postMessage set up.
  • 不需要额外的 iframe 和 postMessage 设置。

Cons:

缺点:

  • Will make the data available across all subdomains (not just www) so if you don't trust all the subdomains it may not work for you.
  • Will send the data to the server on each request. Not great, but depending on your scenario, maybe still less work than the iframe/postMessage solution.
  • If you're doing this, why not just use the cookies directly? Depends on your context.
  • 4K max cookie size, total across all cookies for the domain (Thanks to Blake for pointing this out in comments)
  • 将使数据在所有子域(不仅仅是 www)中可用,因此如果您不信任所有子域,它可能对您不起作用。
  • 将在每次请求时将数据发送到服务器。不是很好,但根据您的情况,可能仍然比 iframe/postMessage 解决方案少。
  • 如果您这样做,为什么不直接使用 cookie?取决于你的上下文。
  • 4K 最大 cookie 大小,域的所有 cookie 总数(感谢 Blake 在评论中指出这一点)

I agree with other commenters though, this seems like it should be a specifiable option for localStorage so work-arounds aren't required.

不过,我同意其他评论者的意见,这似乎应该是 localStorage 的可指定选项,因此不需要解决方法。

回答by Eran Galperin

I suggest making site.com redirect to www.site.com for both consistency and for avoiding issues like this.

我建议将 site.com 重定向到 www.site.com 以保持一致性并避免此类问题。

Also, consider using a cross-browser solution like PersistJSthat can use each browser native storage.

此外,考虑使用跨浏览器解决方案,如PersistJS,它可以使用每个浏览器的本机存储。

回答by URL87

Set to cookie in the main domain -

在主域中设置为 cookie -

document.cookie = "key=value;domain=.mydomain.com"

and then take the data from any main domain or sub domain and set it on the localStorage

然后从任何主域或子域中获取数据并将其设置在 localStorage 上

回答by biology.info

I'm using xdLocalStorage, this is a lightweight js library which implements LocalStorage interface and support cross domain storage by using iframe post message communication.( angularJS support )

我使用的是 xdLocalStorage,这是一个轻量级的 js 库,它实现了 LocalStorage 接口并通过使用 iframe post 消息通信支持跨域存储。(angularJS 支持)

https://github.com/ofirdagan/cross-domain-local-storage

https://github.com/ofirdagan/cross-domain-local-storage

回答by Ayush Baheti

This is how I solved it for my website. I redirected all the pages without www to www.site.com. This way, it will always take localstorage of www.site.com

这就是我为我的网站解决的方法。我将所有没有 www 的页面重定向到 www.site.com。这样,它总是需要 www.site.com 的 localstorage

Add the following to your .htacess, (create one if you already don't have it) in root directory

将以下内容添加到根目录中的.htacess,(如果您还没有,请创建一个)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]

回答by Jamil Noyda

this kind of solution causes many problems like this. for consistency and SEO considerations redirect on the main domain is the best solution.

这种解决方案会导致很多这样的问题。出于一致性和 SEO 考虑,在主域上重定向是最好的解决方案。

do it redirection at the server level

在服务器级别进行重定向

How To Redirect www to Non-www with Nginx

如何使用 Nginx 将 www 重定向到非 www

https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-centos-7

https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-centos-7

orany other level like route 53 if are using

任何其他级别,如路线 53,如果正在使用