2

I have a script which uses a global variable named history. I've confirmed this works fine in Chrome 35 and Firefox, but in Chrome 36 the variable is read only and is populated with a length value, a type value, and a prototype.

The solution for my script is easy enough - I just renamed the variable and all's well - but when I went looking I couldn't find an explanation of what this variable is or why it's been promoted to global in the latest update.

Can anyone point me to more information on this?

For a simple example, try this:

var history = {};

Open the debugger, and note how history isn't an empty object.

Edit: As @Pointy was kind enough to point out, the change here is apparently that history is now read only in Chrome, unlike in Firefox. It has always been global.

Dashiel N
  • 309
  • 2
  • 13
  • 2
    https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history – Pointy Jul 22 '14 at 03:41
  • Thanks. That definitely maps to what I'm seeing, and was understandably hard to find given how common a word history is to search on, heh. Now I just wonder what Chrome is up to promoting it to just `history` instead of `window.history`... I can't be the only one tripped up by that change. – Dashiel N Jul 22 '14 at 03:47
  • 3
    A good strategy when looking for JavaScript or DOM things is to search for "MDN whatever", like "MDN history". – Pointy Jul 22 '14 at 03:49
  • Good idea. Excepting of course that my issue here was Chrome specific... ;p – Dashiel N Jul 22 '14 at 03:51
  • 1
    Yes but MDN has documentation that's surprisingly browser-agnostic. I think the interesting thing here is that Chrome has made that property read-only, while the other browsers haven't. – Pointy Jul 22 '14 at 04:01
  • 1
    Furthermore it might be a good idea to set up a global namespace for your script to work from. For example, `myApp.history`. That way you can be more certain you're not clashing with other globals. – Joshua Jul 22 '14 at 04:20
  • @Pointy, I'm curious whether I was always stomping it and it's just now read-only, or whether it wasn't previously exposed at all except as a property of `window`. – Dashiel N Jul 22 '14 at 04:42
  • 1
    Things that are properties of `window` are the same things as global variables. In other words, `window.foo` is the same as just plain `foo` if `foo` is a global variable. So, for example, `document` is really `window.document`, `location` is `window.location`, `alert` is `window.alert`, `setTimeout` is `window.setTimeout`, etc. – Pointy Jul 22 '14 at 04:44
  • @Josh, that's a great suggestion, and something I should make a habit of I think. – Dashiel N Jul 22 '14 at 04:45
  • That makes total sense @Pointy. And now I try to figure out how I've been writing JS all these years and never realized that... some of those things I've been putting `window` in front of manually all this time, and others I haven't, so I guess I can just chalk it up to learning from many and varied examples. – Dashiel N Jul 22 '14 at 04:47
  • This is one of the examples I’ve listed in [Why is the variable `closed` being logged as `false`, if I define it globally as `0`?](/a/51062916/4642212). The most modern and most rubst approach would be to use modules instead. Something like this can’t happen in a module. Just renaming may just defer the same problem into the future. – Sebastian Simon Oct 06 '21 at 13:14

0 Answers0