javascript - How to share an object between two pages? - Stack Overflow

I wrote a simple JS script to create an object that permit the inizialization of only one instance. If

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!

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!

Share Improve this question edited Jun 18, 2011 at 12:41 dail asked Jun 18, 2011 at 12:35 daildail 211 silver badge3 bronze badges 6
  • 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 municate with the server via ajax or sth. else. – thomas Commented Jun 18, 2011 at 12:42
  • @thomas,i have to put some functions that will be called when the server has some data (et munication)...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 Commented Jun 18, 2011 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 Commented Jun 18, 2011 at 12:54
  • This is an old question, but what about using cookies? – dana Commented Feb 29, 2012 at 22:35
  • 1 Possible duplicate: stackoverflow./questions/1981673/… – Anderson Green Commented Mar 13, 2013 at 21:12
 |  Show 1 more ment

3 Answers 3

Reset to default 2

There may not be a way to do it now, but don't pletely 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

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 municate 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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745264301a4619359.html

相关推荐

  • javascript - How to share an object between two pages? - Stack Overflow

    I wrote a simple JS script to create an object that permit the inizialization of only one instance. If

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信