Kreative Posted February 27, 2017 Share Posted February 27, 2017 (edited) Hello,I would like to define non-replaceable references/getters to the innerWidth and innerHeight properties of window, or whatever they reference and are updated by. This is because currently, I can very easily re-assign, delete or re-define these properties to any other value making them unreliable if they were accidentally tampered with.ResearchIn Mozilla, Object.getOwnPropertyDescriptor(window, x) where x is innerWidth or innerHeight, returns a descriptor of a configurable and enumerable property with a getter and setter containing functions that I can retrieve, bind to window and isolate.However, in Chrome, the function returns a descriptor of a property that does not contain a getter or setter, but rather a value, that magically updates when the inner window is resized given that the property has never been manually re-assigned, re-defined or deleted.Reading Mozilla Developer Network, I have found the specification that describes these two properties is that of the CSS Object Model ( link: https://drafts.csswg.org/cssom-view/#dom-window-innerwidth ). In this specification I have located the `partial interface Window` which defines these view-port properties as `[Replaceable] readonly attribute long` where the [Replaceable] means that the property can be re-assigned and will continue to indefinitely reference the new re-assigned value whilst keeping the same property name, despite it being read-only.This explains the behavior I have seen in Chrome, however there seems to be no means to extract a function or generate a reference that I could potentially hide in a closure or a block and (not 100% but mostly) reliably use it as the true width and height of the inner-window in pixels.Seeing as I can expose and isolate a getter in Mozilla, is there any (perhaps other) way to utilise a slightly safer means of getting the innerWidth and innerHeight of window? Because in terms of JavaScript, the two properties seem to be magical in how they update as doing something like Object.defineProperty(window, 'innerWidth', Object.assign(Object.getOwnPropertyDescriptor(window, 'innerWidth'), {writable: false, configurable: false}) does the same thing as re-assigning and then freezing the property afterward; nothing.Kind Regards, Augustas. Edited February 27, 2017 by Kreative Link to comment Share on other sites More sharing options...
justsomeguy Posted February 27, 2017 Share Posted February 27, 2017 It seems like Firefox doesn't treat it as a read-only property, despite the documentation. Chrome returns a value property without a getter and setter, which is what it sounds like it should do if it's a read-only non-accessor descriptor. Firefox returns a descriptor with a getter and setter but no value, which is the opposite. Their documentation doesn't seem that consistent either. On the MDN page for innerHeight here: https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight Under the Value heading it does say that the property is read-only. But in the list of properties for the Window object here: https://developer.mozilla.org/en-US/docs/Web/API/Window Both innerHeight and innerWidth are not marked as read-only. What problem are you trying to solve though? If you want to read the value of innerWidth or innerHeight for the window you can at any time, what else are you missing? Link to comment Share on other sites More sharing options...
Kreative Posted February 28, 2017 Author Share Posted February 28, 2017 Is it possible to generate a reference or obtain some function that I can hide, that gives innerWidth and innerHeight even if window.innerWidth and/or innerHeight have been replaced? Maybe this could be rephrased as: can the properties be made non-replaceable? Link to comment Share on other sites More sharing options...
Ingolme Posted February 28, 2017 Share Posted February 28, 2017 If the browser doesn't natively do it, there's no way to protect the property. I would operate under the assumption that nobody's messing with window properties. If it's your own code that's overriding the property then fix it. If you're dealing with somebody else's code that does that, you'll have to either rewrite it or stop using it. I cannot imagine a situation where any developer would override these properties, if they're doing so, their code is not worth using. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now