1

I'm trying to change the value for window.navigator.onLine using Object.defineProperty.
The command I ran is :

Object.defineProperty(window.navigator, "onLine", {value: false, configurable: true});

which worked on :
* Edge 13
* Opera 40.0
* Chrome 54.0
* FireFox 49.0

Any ideas why this didn't work? it says that IE11 is suppose to support this method.

Shahar Kazaz
  • 1,146
  • 1
  • 9
  • 21

1 Answers1

2

From the spec it sounds like window.navigator.onLine is a readonly. i.e. if you can't override it by setting the value directly, you might be doing bad things.

If you're doing this to test some behaviour, perhaps you'd be better of mocking the navigator object rather than trying to change a readonly property. (Sorry if that's a bad assumption, feel free to give some more context :).)

JVDL
  • 1,157
  • 9
  • 16
  • I was in the process of writing more or less the same but you were faster :) I'd just like to add that [`window.navigator`](https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator) itself is also supposed to be read-only. – VLAZ Nov 03 '16 at 09:33
  • I'm trying to change this property for a unit-test, and mocking the navigator object doesn't seem to work for me. so this property is a readonly only in IE? – Shahar Kazaz Nov 03 '16 at 09:34
  • @ShaharKazaz it's read-only according to the spec. If some browsers allow changing it, they are not following that. – VLAZ Nov 03 '16 at 09:37
  • @ShaharKazaz if you need to mock something like this, it's best to create a helper method that you can then in turn mock. http://stackoverflow.com/a/7050884/286692 details it pretty well. – JVDL Nov 03 '16 at 10:24