'Redirecting html page with javascript
I have a combo-box which redirects a user to a specific page. To redirect, if I use
window.location.href = ...
when they hit back, it automatically redirects again. And if I use
window.location.replace (...)
they can't go back, because the history gets replaced with the new page.
I would have thought it's a fairly common scenario, so just wanted to ask if anyone knows a trick I'm missing?
Thanks
Solution 1:[1]
just check position of your syntax.
put it inside some function or event.
use below syntax :
window.location = "your_url";
Solution 2:[2]
Ok, finally figured it out. Turns out the jquery MultiSelect I'm using seems to preserve it's value so when I go 'back' into the browse, the MultiSelect kicks off an event as though it's been selected, which kicks off the redirect :( Fixed now by clearing the selected item and then redirecting.
Solution 3:[3]
Try using the onchange event of the select so the redirect wont happen until a user updates the value.
Solution 4:[4]
<script language='javascript'>window.location='index.php'</script>
it works...
Solution 5:[5]
Try this way. Before you do window.location.url, do like:
if (!window.flag) window.location.url
window.flag=true;
So after you instructed for redirect, you are resetting the flag. So when you hit back button on the new page, it goes to the old page, but doesn't redirect.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sid |
Solution 2 | nchaud |
Solution 3 | Tom Elmore |
Solution 4 | zeshan |
Solution 5 | halfer |