2

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!

2
  • You can change the hash with window.location.hash = 'foo'; 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? Commented Jun 24, 2014 at 2:23

2 Answers 2

8

As simple as this

$(location).attr('href', 'index.php');
window.location.hash = "#about";
location.reload();

  1. Select the location where you want to go
  2. Specify to the location in which part of the website you want to go
  3. Reload the website in order to make work the hash function.
Sign up to request clarification or add additional context in comments.

Comments

6

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);
    }
});

1 Comment

Thank you! But is it possible without using the "click" function? going to the #load will automatically redirect with a delay of 5 seconds without clicking?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.