How to store user input URLs (anchor links) and redirect the user using window.location?


I managed to create a condition to detect anchor link, but after that how do I store the anchor link in my anchor_num variable, so that I can redirect the use to specific url?

<script type="text/javascript">



        var anchor_num = "anchorString";
        window.location = "#"+ anchor_num;

    } else
            //redirect to default page


By appending or changing hash the browser wouldn't refresh your page. You should call window.location.reload() after you've set the hash:

window.location = "#"+ anchor_num;