Check if window is available before using it#104
Check if window is available before using it#104pimterry merged 4 commits intopimterry:masterfrom thomasconner:master
Conversation
Some JavaScript platforms break because of the use of window. Below are some platforms I can think of. https://www.nativescript.org/ http://www.appcelerator.com/mobile-app-development-products/ https://facebook.github.io/react-native/ (Possibly)
| window.document.cookie = | ||
| encodeURIComponent(storageKey) + "=" + levelName + ";"; | ||
| } catch (ignore) {} | ||
| } |
There was a problem hiding this comment.
Sure, this is reasonable. I'd rather flip it and add a guard clause though, rather than indenting the whole function. Can you convert both of these into something like the below instead?
if (typeof window === undefinedType) return;
[...rest of the function...]|
Sorry, JSHint actually doesn't like that example, so the build broke. I disagree though, so I just went and added a quick patch to relax that, and then there was an actual bug, so I fixed that while I was there too. Anyway, all looks good to me now, merging! Thanks for contributing 😃 |
|
@thomasconner Hmm, wait actually. Looking closer at this, I'm not sure why it's required. The following lines that access Are you fixing this because this causes an actual issue you can reproduce somewhere? |
|
Yes. When I was using I will try and put a project that reproduces the issue. |
Some JavaScript platforms break because of the use of window. Below are some platforms I can think of.
https://www.nativescript.org/
http://www.appcelerator.com/mobile-app-development-products/
https://facebook.github.io/react-native/ (Possibly)