Tomas Andrle Tomas Andrle - 1 year ago 74
Javascript Question

Javascript to redirect from #anchor to a separate page

I have a set of links with #anchors pointing to a single webpage and I would like to smoothly move to a model with a separate webpage for each of those links. I want the old links to keep working using a redirect.

Old link style:


New link style:


I know that the server does not receive the #anchor name in the request but Javascript might.

Is it possible to automatically redirect from /all_products#A to /products/A using Javascript?

JQuery would be fine, it's being used on the site anyway.

Answer Source

I hope this can help :)

var urlSplit = document.URL.split("#");
if (urlSplit[1]) {
    location.href = "" + "/" + urlSplit[1];
else {
    location.href = "";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download