Redirects for jQuery
Redirects for jQuery
Hi,
Today I realized that redirects in JavaScript suck horribly. Somehow it
turns out that noone seems to encounter the same issue that I ran into
today.
Take this file, try it online (works) and then offline (does not work
anymore - race condition triggered).
<html>
<head>
<title>Testing JS redirection</title>
<script type="text/javascript">
function redir() {
document.location = '../test';
return false;
}
</script>
</head>
<body>
<form action="http://jquery.com/" method="POST"
onsubmit="return redir();">
<input type="text" name="content" />
</form>
</body>
</html>
You see that you first get redirected to the specified page and then
you'll be redirected to jquery.com, because the false was never
returned.
To solve this, you can use a strange hack, by creating a function which
takes care to return and triggers the redirect after it returns.
So, try it again:
<html>
<head>
<title>Testing JS redirection</title>
<script type="text/javascript">
function real_redir() {
window.clearInterval(redir);
document.location = '../test';
}
function redir() {
redir = window.setInterval("real_redir()", 100);
return false;
}
</script>
</head>
<body>
<form action="http://jquery.com/" method="POST"
onsubmit="return redir();">
<input type="text" name="content" />
</form>
</body>
</html>
I'd love to see jQuery providing such a helper which automatically
creates such a function and redirects. What are your thoughts about
this?
regard,
Marek