I've seen site redirection within a certain delay/time limit however is it possible to do that within a page that only has hash urls?
What is the code for redirecting a hash location within a page? For instance we are currently in #home then when I click on #load, it waits 5 seconds then redirects to #about - all within just one page just different hashes/#.
Thank you!
I've seen site redirection within a certain delay/time limit however is it possible to do that within a page that only has hash urls?
What is the code for redirecting a hash location within a page? For instance we are currently in #home then when I click on #load, it waits 5 seconds then redirects to #about - all within just one page just different hashes/#.
Thank you!
Share Improve this question asked Jun 24, 2014 at 1:42 CattarinaCattarina 611 gold badge2 silver badges10 bronze badges 2-
You can change the hash with
window.location.hash = 'foo';
– christian314159 Commented Jun 24, 2014 at 2:04 - Someone left an answer: You can use setTimeout to delay any action. The following will allow for a five second delay after clicking the load link (assuming a loadLink id). Nothing special about hash links here, would work the same on any link. $('#loadLink').on('click', function () { setTimeout(function () { //navigate to about after 5 seconds }, 5000); }); The second parameter after the function to execute is the time to wait in milliseconds. I'm using that however what is the code that auto redirects without the "on click" function? – Cattarina Commented Jun 24, 2014 at 2:23
2 Answers
Reset to default 8As simple as this
$(location).attr('href', 'index.php');
window.location.hash = "#about";
location.reload();
- Select the location where you want to go
- Specify to the location in which part of the website you want to go
- Reload the website in order to make work the hash function.
Well, if you click on an anchor already associated with another hash (would be curious, but this is what I understood), you will first have to prevent the defautl behavior, then you can redirect.
Assumed HTML
<a href="#load">some link</a>
JS (once document is ready)
$('a[href="#load"]').click(function (e) {
e.preventDefault();
setTimeout(function () {
window.location.hash = 'about';
}, 5000);
});
EDIT: "going to the #load will automatically redirect with a delay of 5 seconds without clicking"
$(window).on('hashchange', function () {
if (window.location.hash == '#load') {
setTimeout(function () {
window.location.hash = 'about';
}, 5000);
}
});