Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window object and properties persistance

Tags:

javascript

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 come by. Thanks.

like image 627
RonS Avatar asked Jun 05 '14 11:06

RonS


2 Answers

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.

like image 61
Jack_of_All_Trades Avatar answered Oct 17 '22 16:10

Jack_of_All_Trades


All the global JavaScript variables, functions & objects automatically become 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

like image 27
Aniruddha Chaudhari Avatar answered Oct 17 '22 16:10

Aniruddha Chaudhari