Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: window.onpopstate is not a function #312

Open
rohan-deshpande opened this issue Jul 8, 2015 · 11 comments
Open

Uncaught TypeError: window.onpopstate is not a function #312

rohan-deshpande opened this issue Jul 8, 2015 · 11 comments

Comments

@rohan-deshpande
Copy link

Director is throwing this error at times, from this line in the code

this.history === true ? window.onpopstate() : window.onhashchange();

Not too sure about this, a bit strange that the window doesn't have this function no?

Edit

Right so I just commented out the setTimeout and replaced it with window.onpopstate = onchange; and this fixed everything. Hopefully that's all that needs to be done.

@alexanderGugel
Copy link

Running into the same issue. Can't reproduce on a consistent basis.

Is there an update on this?

@rohan-deshpande
Copy link
Author

Remove the call to setTimeout and you should be fine unless you want to support older versions of chrome.

I'm pretty sure they fixed this bug a long time back so you should be safe to remove it.

@asazernik
Copy link

This bug is definitely fixed - the fix shipped in Chrome 34, way back in April 2014. I find it very unlikely that anyone is still running a browser with this issue.

I don't have the setup to test this easily, but @rohan-deshpande, if you're confident in your change, could you make a pull request for it?

@asazernik
Copy link

This is an actual issue, and not just a spurious error message, because it causes route changes that occur within half a second of page load (for example, because of an in-app redirect from a root URL) to not fire the appropriate Director handlers.

@vogelsgesang
Copy link

Actually, PR #279 fixes this issue but it was not merged so far.

@krrg
Copy link

krrg commented Oct 26, 2015

I'm experiencing the same issue when I try to do a programmatic redirect. What is the status on #279 ?

@rohan-deshpande
Copy link
Author

Have asked the guy in that thread to post his solution to the subsequent iOS issue he was experiencing as a result of this fix for the "fix".

@bunchjesse
Copy link

I've worked around this issue with the following shim of setRoute. It waits for onpopstate to become available before actually calling into director.

  (function() {
    const oldSetRoute = router.setRoute;
    router.setRoute = function() {
      if (window.onpopstate) {
        oldSetRoute.apply(router, arguments);
      } else {
        setTimeout(function() {
          router.setRoute.apply(router, arguments);
        }, 10);
      }
    }
  })();

@SpaceK33z
Copy link

For anyone still interested in this, I've fixed this issue in my fork and published it on npm.

@avnersorek
Copy link

@bunchjesse using your solution 👍
But please notice that the arguments inside the timeout function should be changed to a variable with the original setRoute arguments

@nickrussler
Copy link

nickrussler commented Mar 9, 2020

Shouldn't it be possible to assign the onpopstate function earlier? Since the Chrome bug should be long fixed by now.

window.onpopstate = function onchange(onChangeEvent) {
  for (var i = 0, l = Router.listeners.length; i < l; i++) {
    Router.listeners[i](onChangeEvent);
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants