Prevent page redirect when using hash tag
-
Is there any way to redirect a page to a specific hash but prevent the redirect if the page is reloaded with a hash already present? Preferably with javascript? For example: typing in www.mysite.com redirects to ---> www.mysite.com/index.html#news then the user clicks a link that navigates to a new location further down the same page to: www.mysite.com/index.html#articles what I want is to prevent the redirect upon refresh so that if the user refreshes the page they remain on #articles rather than being redirected back up to #news. essentially the only purpose for the redirect is to take people to #news only if the url is typed in without a hash(#).
-
Answer:
You can inspect the window.location.hash property and set the location href based on its value. For example: <script type="text/javascript"> // Redirect to "#news" if there is no hash in the URL. if (window.location.hash == '') { window.location.href = 'http://www.mysite.com/index.html#news'; } </script> Incidentally, it seems that you can use either https://developer.mozilla.org/en/DOM/window.location or https://developer.mozilla.org/En/Document.location which appear to do the same thing. http://stackoverflow.com/questions/948227/should-i-use-window-navigate-or-document-location-in-javascript (window or document).
Robert at Stack Overflow Visit the source
Other answers
Anchor redirects are a client-only construct. The server has absolutely no knowledge of the fact that someone clicked "#articles" -- and the URL does not change when they do so, so refreshing will always land you on the same URL that got you there. The solution is to rewrite the URL when they click on an anchor link. You can change it by assigning to window.location.href. So add a click handler to any hash anchor, and change the URL to incorporate the new in-page anchor tag.
jamietre
Related Q & A:
- Why would Arial be placed before Helvetica when using font-family in CSS?Best solution by Quora
- How to link websiteurl in anchor tag in jsp page?Best solution by Stack Overflow
- Why thread is not working as concurrent when using pthread_detach(?Best solution by stackoverflow.com
- Why separate worksheets when using excel?Best solution by Blurtit.com
- When using Harvard Referencing?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.