2

I wrote a simple JS script to create an object that permit the inizialization of only one instance. If the object is already inizialized it return is without recreate it again.

This is my: myobj.js

var Singleton = {

  initialized : null,

  init: function(){

    console.log("new obj...");

    this.initialized = {
      'status' : 'initialized'
    }

  },

  getInstance : function(){

    if (!this.initialized) this.init();

    return this.initialized;

  }

}

Then I have create a test.html page to test this script:

<script src="myobj.js"></script>

<script>
var uno = Singleton.getInstance();
var due = Singleton.getInstance();

(uno===due) ? console.log("equals") : console.log("not equals");
</script>

All works good, only one object is created.

My question is: can I share this object "Singleton" between more HTML pages?

without recreate it in different pages?

If I have (for example) 100 tabs opened on the browser, I would like to use the same object without having the same 100 objects.

Thank you!

dail
  • 21
  • 1
  • 3
  • I dont think so because every page in your browser has its own "sandbox" and cannot access any information on client-side exepct of it's own information. The only possibility is via serializing your singelton and put it in a cookie and deserialize it on loading on each page or: manage your singleton on the server-side and communicate with the server via ajax or sth. else. – thomas Jun 18 '11 at 12:42
  • @thomas,i have to put some functions that will be called when the server has some data (comet communication)...so this object will have function and connection. I don't want to connect to server from every single page...is it possible to store the serialized object on the server and then call it with ajax, and then deserialize? – dail Jun 18 '11 at 12:50
  • Serializing/deserializing would mean exactly the same thing as creating separate copies explicitly - when you deserialize a serialized form of an object, you get a **copy** of the object. Thus, that will not result in your having one object shared among many pages. – Pointy Jun 18 '11 at 12:54
  • This is an old question, but what about using cookies? – dana Feb 29 '12 at 22:35
  • 1
    Possible duplicate: http://stackoverflow.com/questions/1981673/persist-javascript-variables-across-pages – Anderson Green Mar 13 '13 at 21:12
  • i thought that the answer just didnt exist this long ago, but yes.. `var newWindow=window.open(location.href); newWindow.Singleton=Singleton` – The Bomb Squad Mar 18 '21 at 23:38

3 Answers3

2

There may not be a way to do it now, but don't completely give up hope; there may be a way to do this soon. There are several good reasons for wanting to share JS objects between pages, but one of them that I've thought about is sharing large persistent data structures between many WebWorkers.

Here are two papers from Berkeley on this very topic, from 2010, so you can see that you're not the only one who wants it...

Berkeley/Google: Object Views: Fine-Grained Sharing in Browsers

Berkeley/Microsoft: Secure Cooperative Sharing of JavaScript, Browser, and Physical Resources

Bill Burdick
  • 935
  • 10
  • 15
  • Also, you can use `window.name` to [share variables between multiple pages on the client side](http://stackoverflow.com/a/1981681/975097). This is known as a "JavaScript session". (However, I don't think it's yet possible to share an object between multiple tabs that are already open.) – Anderson Green Mar 13 '13 at 21:17
2

I'm afraid it's not possible across all browsers at the time of writing without the user installing anything. Two partial solutions:

1) Use a browser extension to store the state

Browser extensions can communicate with your pages via the host browser's API and maintain shared state. However, the user must install them.

2) Use LocalStorage

This is shared between pages on the same domain, but only stores strings. You can use JSON.stringify to convert an object to a string and place it on localStorage. http://diveintohtml5.info/storage.html

jazmit
  • 5,170
  • 1
  • 29
  • 36
-1

No, it's not possible. You can't keep object between page reloads and you can't share objects between different browser tabs. With cookies you can store some simple data this way, but that's all.

genobis
  • 1,081
  • 9
  • 13