Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global object when window.global is defined #1189

Open
yanatan16 opened this issue Apr 3, 2015 · 17 comments
Open

global object when window.global is defined #1189

yanatan16 opened this issue Apr 3, 2015 · 17 comments
Labels

Comments

@yanatan16
Copy link

It seems that the global object can be window.global as opposed to window when it exists. This is a problem when some third party javascript decides to insert <div id="global">, and now two scripts can't refer to the same global object.

@jmm
Copy link
Collaborator

jmm commented Apr 3, 2015

Thanks for the report. Maybe global should only be allowed to be the global object when ! window.

@JamesKyburz
Copy link

Or maybe adding a check that global.appendChild is not defined

@jmm
Copy link
Collaborator

jmm commented Apr 13, 2015

@JamesKyburz Thanks for the suggestion. I was also wondering if a more specific test would make sense, but I was thinking of going the other way, like maybe checking for window.document or even window.document.implementation (or something), in case there's some plausible reason for there to be a global.window in node code. If the test was for global.appendChild, some random script could still break things by just setting window.global = {}, right?

@JamesKyburz
Copy link

@jmm true that. Maybe go with your first comment. A too big change could as you pointed out break stuff.

@nmn
Copy link

nmn commented May 20, 2015

I think the convention of global being the global is pretty good and doesn't get in the way.
You can get to other global properties like document like so:
global.document

and in case there is an element with the id global, you can get to it by doing:

global.global

I think trying to access global properties directly is an anti-pattern anyway. Any global variables should be explicitly be accessed by using global.

@jmm
Copy link
Collaborator

jmm commented May 20, 2015

@nmn I think you may have misinterpreted the issue. The problem is that if there is a window.global defined (which may happen just by virtue of a <whatever id="global"> element in the document) when the bundle executes, then inside the bundle global !== window, so there would be no global.document.

@nmn
Copy link

nmn commented May 20, 2015

I think that in the bundle global === window. This is because global will be set as a variable by browserify and will mask the outer variable set by the DOM element. Further, I think global is set to the value of this so it should be foolproof.

@jmm
Copy link
Collaborator

jmm commented May 20, 2015

@nmn Have you looked at the source? Or verified those theories by testing it?

@vvo
Copy link

vvo commented May 29, 2015

It seems that the global object can be window.global as opposed to window when it exists. This is a problem when some third party javascript decides to insert

If the underlying website have a <div id=global> this happens too and then the global reference used in a browserified file will be useless.

Maybe The check for global should not be if (typeof global !== undefined) .. but rather if Object.prototype.hasOwnProperty.call(window, 'global') ..

what do you think?

@nmn
Copy link

nmn commented May 31, 2015

@jmm Sorry, my knowledge about this was based on an old version of browserify.
However, I do think, that global should always be made to point to the window object. As other globals will always still be accessible.

@yanatan16
Copy link
Author

@vvo I agree that if (typeof global !== undefined) is the culprit (here?). What if we reversed the order of ternary operators so that window was tested first, instead of global?

@vvo
Copy link

vvo commented May 31, 2015

Would be better yes @yanatan16

@ltressens
Copy link

More than a year has passed before this issue was created.
Any fresh news ?

@acemtp
Copy link

acemtp commented Jun 15, 2016

Any news about this problem? We just spent 2 hours to find that one of our client had a <div id="global"> that broke sockjs because they use browserify...

@mrak
Copy link

mrak commented Jul 21, 2016

We'd like to see this issue get fixed as well.

It's unfortunate in situations where your company or team does not have control over the DOM context that has an element with id="global" in which your code is running.

@jmm
Copy link
Collaborator

jmm commented Jul 22, 2016

@ltressens @acemtp @mrak FWIW this would need to be fixed in insert-module-globals and, while there's been no recent news, most of the discussion is in browserify/insert-module-globals#48. As you can see, I think allowing things to break due to a random <someElement id="global"> is too fragile and something should be done.

@JoeZ99
Copy link

JoeZ99 commented Dec 14, 2016

Looks like there's something missing that would satisfy collaboratos, so the browserify/insert-module-globals#48 PR doesn't get to be applied. This is somehow painful for all of us that are building libs to work in non-controlled DOM environments.

Has anyone at least come with some workaround in the meanwhile???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

10 participants