3

Not sure what I'm doing wrong, but this small bit of code does not work:

window.history.pushState("foo", "foo", "foo");

It will generate the following error in firefox 29:

TypeError: window.history.pushState is not a function
Wesley Brian Lachenal
  • 3,442
  • 8
  • 40
  • 77
nablex
  • 4,319
  • 4
  • 29
  • 48
  • 1
    did you have a look [Here?](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history) – Banana Jul 11 '14 at 11:06
  • What browser are you using when you get this error? Check it if your browser [supports the history api](http://caniuse.com/#feat=history). – complex857 Jul 11 '14 at 11:07
  • @complex857 The browser is (as mentioned in the post): firefox 29 – nablex Jul 11 '14 at 11:09
  • @Banana Yes, mozilla docs are usually my first stop – nablex Jul 11 '14 at 11:09
  • oh, looks like i can't read (-: – complex857 Jul 11 '14 at 11:10
  • @nablex which release of the firefoxes is that? firefox/firefox aurora/firefox beta/firefox for organizations ? – Banana Jul 11 '14 at 11:15
  • @nablex i have tried your code in firefox 29.0 and it works fine, are you redefining `window` / `history` / `pushState` anywhere in your code? – Banana Jul 11 '14 at 11:18
  • @Banana Yes, I have done it from scratch and that seems to be the problem, one of the scripts i was including defined a variable "var history". Out of curiosity: why is "window.history" overwritten by "var history"? Are all vars by default in window scope? – nablex Jul 11 '14 at 11:21
  • 2
    @nablex yes they are. variables that you define directly under a script tag are in the window scope. you can try it, open developer console anywhere and define `var banana=2;`, and then type in `window.banana` and you will receive 2 – Banana Jul 11 '14 at 11:25
  • Nice to know :) Could you add it as an answer? Then I can accept it. – nablex Jul 11 '14 at 11:35
  • @nablex you can add an answer yourself and accept it. Then there is no need to read all the comments to understand that this issue is solved already. – hg. Aug 01 '14 at 08:12
  • Hope it will help. Refer balupton's answer. http://stackoverflow.com/questions/4015613/good-tutorial-for-using-html5-history-api-pushstate – Janty Aug 01 '14 at 10:11

2 Answers2

1

Apparently one of the scripts I included had a declaration:

var history = ...;

Unbeknownst to me, all vars on the root actually live in the window scope so the custom history var actually overwrote the original window.history.

nablex
  • 4,319
  • 4
  • 29
  • 48
0

Please try as

Suppose http://mozilla.org/foo.html executes the following JavaScript:

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");

It should work as per Mozzila.

Please find more On Mozilla Link

Janty
  • 1,588
  • 2
  • 12
  • 28