Reputation: 53
$('#target').click(function() {
window.location.replace("http://www.google.com");
return false;
});
How can I tell chrome and all other browsers to stop what they're doing and immediately redirect to another web page?
Firefox 8.0 executes the window.location.replace immediately, as I want it to.
When I step through the code above, Chrome 15.0.874.121 flies right past the window.location code and the return false and then moves into subsequent Jquery functions (in other words, Chrome completes the entire call stack and THEN replaces the URL)
I'm trying to execute the redirect in a number of different areas, the biggest problem is that the additional Jquery can add 3-5 SECONDS of time to the redirects, especially from within an .ajax success call.
I'm using jQuery 1.7.1 and have no javascript errors
I've tried:
No matter what I do, Chrome refuses to simply GO to another web site without finishing its call stack first. Please help.
Upvotes: 2
Views: 1681
Reputation: 51
I had the exact same problem. I ended up using an if else block and it worked.
Coffeescript
if Number(deck.length) == Number(deck.counter)
window.location.href = "/rounds"
else
whatever...
Javascript
if (Number(deck.length) === Number(deck.counter)) {
return window.location.href = "/rounds";
} else {
Upvotes: 0
Reputation: 53
$('#target').click(function() {
window.location.replace("http://www.google.com");
//THROW an exception is the only solution I've found so far
throw "Reloading Page";
});
Unfortunately, I have not found a better answer. It is NOT a jquery issue after all. I've trimmed everything down: removed all extensions in chrome, removed all external files, made a completely blank .html file with bare minimum javascript and a simple onclick... and Chrome STILL blasted through the redirect.
SO: As of now, my only answer is to throw an exception to tell Chrome to stop running JS just after the redirect.
Thanks to Gijs
Edit: Unfortunately, Chrome pauses on thrown exceptions be default so this turns out to not actually be a deployable solution.
Upvotes: 1
Reputation: 13614
Have you tried event.preventDefault
?
$('#target').click(function(evt) {
evt.preventDefault();
window.location.replace("http://www.google.com");
return false;
});
Upvotes: 0
Reputation: 1687
Why don't you use simple Javascript and only do
document.location = "http://google.com";
Upvotes: 0