How to redirect URL on refresh in Angular?

How do I redirect an anchor URL to another URL on my website?

  • EG: http://www.example.com/page1.html#page2, needs to redirect visitors to: http://www.example.com/page2.html. - If you're wondering why, I warn you it's complicated, so its not necessary to try to make sense of this bit if you're not interested haha: #page2 is a sort of pseudo-url which is activated when the user clicks on a link which doesn't go to another page, it just uses javascript to change the page instead (for fast loading) & then re-anchors the user to the top of the page, to give the impression that they've gone somewhere else. But if the user wants to link directly to page2.html, and they use the URL, page1.html#page2, they will just go to page1.html, with the anchor activated at the top, but not the javascript activated that changes the content. So I need to redirect them to an actual HTML version of said page! The HTML versions are only there to solve this problem, (or for people with javascript disabled), and the only reason i'm complicating it with all this javascript is because the pages load incredibly slowly otherwise due to the content! =)

  • Answer:

    You can't, because the anchor isn't sent to the webserver as part of the request. (As internal page navigation it's held to be client-side behaviour so browsers handle it themselves when the page has loaded.) That said, it's probably a good thing based on your stated need. You want to try something convoluted to add to a javascript kludge for having pages that are too large/complicated. I really would be looking at the design and implementation of my pages if I were you.

Now or Never at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

I agree with the other commenter-- you've got bigger problems than this. That said, here's how you'd do something like what you mention. Note this executes directly in the head and so will run as soon as the page starts loading (before the content loads). Don't put it at the bottom of the page, or in a function that runs on page load or something, as that will mean the page content will have to load before it executes which kind of defeats the purpose. You may also want to add some extra error checking before going to the new page, to guard against things like "#foo" or other garbage that doesn't map to a real page on your site. <head> <script type="text/javascript"> if (location.hash) { // if there is no "#pagex" on the url, location.hash == "" var pageName = location.hash.substring(1); // chop off the leading "#" // using location.replace REPLACES the current history entry, instead of adding a new one. // this ensures that when the user hits the "back" button the browser will return to // the last page the user saw, instead of to this one which keeps redirecting them again. location.replace(pageName + ".html"); } </script> </head>

Related Q & A:

Just Added Q & A:

Find solution

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.