NS_BINDING_ABORTED Javascript window.location.replace()
Asked Answered
M

1

8

I'm writing some Javascript code and I'm trying to change the current page as the result of the user clicking a button. I'm using this snippet of code:

window.location.replace("/customer/order/12");

containing the relative URL within my site that I want to navigate to. When this code runs (looking at it in Firebug), the url string looks correct, but the page just does a refresh to what it's currently on. Looking at the headers with HttpFox the first thing I see is a result of (Aborted), Type: NS_BINDING_ABORTED. However, if I issue this command:

window.location.replace("/customer/order/12");

from Firebug, the browser goes to the correct url that I specified ("/customer/order/12").

Can anyone help me determine what's going wrong here?

Thanks in advance! Doug

Moore answered 12/4, 2010 at 19:54 Comment(0)
I
26

You may get NS_BINDING_ABORTED when a load is interrupted by something else, typically a page navigation.

How are you calling this method? If it's in response to a click on a link or button, you may be forgetting to return false to cancel the default action. In which case first your script would start to navigate to /customer/order/12, then the link or form would be followed, causing a navigation that cancels your script's one.

Invaginate answered 12/4, 2010 at 20:53 Comment(2)
bobince, Thanks for the "slap upside the head", and all I can say is D'OH! Putting the return false at the end of my click handler resolved the problem. No matter how many times this happens to me, I still forget that sometimes. Is it just me, or does it seem like Javascript handles event handler return values inconsistently depending on what the handler does? Anyway, thanks again for your help! DougMoore
It's consistent, it's just that there's such a range of different default actions for the various events, some of the default actions cause other events, some of them can't be cancelled and some of them don't work right in some browsers. It's great fun!Invaginate

© 2022 - 2024 — McMap. All rights reserved.