javascript - Window object and properties persistance - Stack Overflow

I know the Window object is the "master" object of the browser (tab) to which everything is a

I know the Window object is the "master" object of the browser (tab) to which everything is appended - core methods, globally declared variables, functions, even the DOM. It is above everything.

When I go to a different page in the same browser window (tab) I suppose the window object remains the same (only the dom changes), because the history and other stuff is accessible. Why I don't quite grasp is why the global variables that are attached to the window object (even using window.myvariable) don't persist.

To me the only possible explanation is because it is made this way. If so what happens, do the "non core" window elements (methods and variables that were set by the code) get erased; is every new page visit a new instance of the window object (sounds the most obvious way to me) or ...?

I can't find any useful info on this matter, usually people only know that you can't pass variables between pages (except cookies, web storage, window title), but why/how (the mechanics, not reasons) this happens are hard to e by. Thanks.

I know the Window object is the "master" object of the browser (tab) to which everything is appended - core methods, globally declared variables, functions, even the DOM. It is above everything.

When I go to a different page in the same browser window (tab) I suppose the window object remains the same (only the dom changes), because the history and other stuff is accessible. Why I don't quite grasp is why the global variables that are attached to the window object (even using window.myvariable) don't persist.

To me the only possible explanation is because it is made this way. If so what happens, do the "non core" window elements (methods and variables that were set by the code) get erased; is every new page visit a new instance of the window object (sounds the most obvious way to me) or ...?

I can't find any useful info on this matter, usually people only know that you can't pass variables between pages (except cookies, web storage, window title), but why/how (the mechanics, not reasons) this happens are hard to e by. Thanks.

Share Improve this question edited Jun 5, 2014 at 12:10 RonS asked Jun 5, 2014 at 11:59 RonSRonS 911 silver badge3 bronze badges 4
  • 3 Your assumption is wrong, every tab has its own window object. Also, above window there's browser itself, where for example history and many other objects are basicly ing from. – Teemu Commented Jun 5, 2014 at 12:05
  • Edited so it mentions tabs. I quoted the "master" word to indicate that it is not a true master, but I see your point, it is not above everything. But in the same tab it should still be the master object to what everything on the page is appended and which all the webpage interaction with the browser goes through. Hence "master". – RonS Commented Jun 5, 2014 at 12:16
  • My point was actually "every tab has its own window object", the browser is there just to point, where window gets a part of its objects. Also you can pass variables (or at least values) to other windows, this is districted only by security features. If you open a page from the same domain in a new tab/window using JS, you can also interact with its window object. There's PostMessage for interacting with even cross-domain iframes etc. – Teemu Commented Jun 5, 2014 at 12:26
  • I'm not following you well here. By tab you mean an actual browser tab (the open with ctrl+T kind), or are you reffering to a webpage as in every visit to a webpage makes a new instance of the window object of which the "prototype" (in JS terms) holds the methods, pointers etc to the browser. I would like a simplified info on the mechanics. Eg. i'm on google., then I go to yahoo.. Do the (if any) global variables that were set on google. somehow reset (clear) or does the browser make a new window instance for yahoo.. So is the window cleaned or do we install a new one :) – RonS Commented Jun 5, 2014 at 12:46
Add a ment  | 

2 Answers 2

Reset to default 3

All the global JavaScript variables, functions & objects automatically bee member of the window object and they are persisted as long as the the new page is not loaded/visited in the same browser window (tab).

Whenever new page is loaded or we redirect to new page within same tab then scope of these functions and variables from previous document ends and they are removed from the window object. And globally declared variables and functions for newly loaded document (from scripts associated with document) gets attached to the window object

Every tab in your browser is independent window object and has its own set of global variables and thus your assumption of sharing window object is not correct. Your javascript is not and should not be allowed to SEE between tabs. If that was possible then imagine one webpage you open sniffing data and other information between tabs. Your tabs are not allowed to do your browsing history sniffing ( they can get the length of the history, I think, though).

How can the browser be secure (If you are browser's vendor)?

  • First, it can chose not to support certain capabilities, period such as reading client's file system arbitrarily.

  • Second, restrict some of the features they provide.

You might want to take a look at Same-Origin Policy and certain restrictions that are relaxed that might be helpful.

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

相关推荐

  • javascript - Window object and properties persistance - Stack Overflow

    I know the Window object is the "master" object of the browser (tab) to which everything is a

    15小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信