function test(){
alert("This message should show");
window.location.href = "http://www.google.com";
alert("This message should NOT show");
}
<href="#" onClick=test()>Click Me</a>
- Step through using Firefox(v8): The second message does NOT show.
- Step through using chrome (v15, v16): BOTH alerts show.
Chrome blows past the redirect and continues through the rest of the call stack (in this case, it finishes the onClick) before redirecting.
- No JavaScript Errors.
- All extensions disabled.
- I’m using a blank html file with no external files loaded.
Does anyone know of a way to get chrome to execute the window.location change immediately?
Pretty sure the specs give you no guarantees for this one, so I’m not too surprised. The obvious solution would be to throw an exception right after. Assuming you don’t have catch blocks littered around your code, that ought to work:
(Yes, it’s ugly. Sorry!)
Edit: fiddles: without throw, with throw