1

With JavaScript/HTML5's "LocalStorage" feature, if you're on myDomain.com:81 and you set a value in local storage, but then redirect to myDomain.com, the local storage will be different, and the value will be lost.

How can I store a simple value that exists across all domains in my browser?

If it makes a difference, this is for a Chrome extension.

qJake
  • 16,821
  • 17
  • 83
  • 135
  • http://stackoverflow.com/questions/263010/whats-your-favorite-cross-domain-cookie-sharing-approach – Satish Jun 29 '11 at 18:08
  • 1
    None of those have concrete examples... all I want to do is store a string temporarily to be able to access it on the *same* domain on a *different* port ... I can't belive this is that difficult... Not to mention, I'm not really talking about cookies, but rather local storage from an extension. – qJake Jun 29 '11 at 18:09

5 Answers5

4

Chrome extensions have their own dedicated localStorage that works across all domains. You can use it by accessing localStorage from a background page (or any other extension page).

serg
  • 109,619
  • 77
  • 317
  • 330
  • What if the page I'm working on is a content script injected into the page? Or... should I have my content script call a method on my background page to save and load this value? – qJake Jun 29 '11 at 18:12
  • @SpikeX Yep, you need to be sending messages between content scripts and a background page to retreive those values. – serg Jun 29 '11 at 18:16
1

You're running in to the same-origin policy. You can probably use normal browser cookies to achieve what you want - they have more lax restrictions, and can be designated to be shared across subdomains, ports, etc.

digitalbath
  • 7,008
  • 2
  • 17
  • 15
  • I would recommend this along with backing up to localStorage, unifying your work under a simple get(setting) and set(setting, value) api – Devin Rhode Nov 29 '11 at 02:30
0

Here's a github project using flash cookies and javascript: https://github.com/nfriedly/Javascript-Flash-Cookies

Joe
  • 80,724
  • 18
  • 127
  • 145
0

I would assume that this is only possible via server-side stuff. The whole point of multiple domains is that they are unique.

A port is a different service. e.g., a different server. Sorry, but that's just the way it is. You can't have two distinct services and expect the browser to treat them as the same one.

rockerest
  • 10,412
  • 3
  • 37
  • 67
0

There's a globalStorage that one can use in place of localStorage, but it's only supported on some browsers. (Chrome, sadly, does not appear to be one of these.)

Here's another approach that circumvents the security sandbox to allow this sort of thing, but which requires the cooperation of a server:

http://www.nczonline.net/blog/2010/09/07/learning-from-xauth-cross-domain-localstorage/

Sean McMains
  • 57,907
  • 13
  • 47
  • 54