6

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:

/all_products#A
/all_products#B
/all_products#C

New link style:

/products/A
/products/B
/products/C

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.

Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92

5 Answers5

13

I added this new answer to include some best practices for both extracting the hash from the url and doing a redirect.

// Closure-wrapped for security.
(function () {
    var anchorMap = {
        "A": "/products/A",
        "B": "/products/B",
        "C": "/products/C"
    }
    /*
    * Best practice for extracting hashes:
    * https://stackoverflow.com/a/10076097/151365
    */
    var hash = window.location.hash.substring(1);
    if (hash) {
        /*
        * Best practice for javascript redirects: 
        * https://stackoverflow.com/a/506004/151365
        */
        window.location.replace(anchorMap[hash]);
    }
})();
janpio
  • 10,645
  • 16
  • 64
  • 107
logan
  • 3,416
  • 1
  • 33
  • 45
3

Put this as close to the top of your HTML <head> as you can so that it can execute before the rest of the page resources download:

<script>
function checkURL() {
    var old_path = '/all_products';
    if (window.location.pathname != old_path) {
        // Not on an old-style URL
        return false;
    }
    // Some browsers include the hash character in the anchor, strip it out
    var product = window.location.hash.replace(/^#(.*)/, '$1');
    // Redirect to the new-style URL
    var new_path = '/products';
    window.location = new_path + '/' + product;
}
checkURL();
</script>

This will check the current page URL and redirect if it matches the old-style path.

This code makes use of the window.location object which contains all the parts of the current URL already split up into component parts.

Making this script more generic is left as an exercise for the implementer.

James Wheare
  • 4,650
  • 2
  • 26
  • 22
2

I hope this can help :)

var urlSplit = document.URL.split("#");
if (urlSplit[1]) {
    location.href = "http://www.example.org" + "/" + urlSplit[1];
}
else {
    location.href = "http://www.example.org";
}
Gregory
  • 501
  • 6
  • 13
  • Please note: There is an issue with using this within a "default" page in a directory for ie8: It loses the hash value. – Gregory Aug 31 '09 at 05:36
  • You should prefer [this method to extract the hash](http://stackoverflow.com/a/10076097/151365) and [this method to redirect](http://stackoverflow.com/a/506004/151365) to avoid the problems stated in the linked articles. I added [an updated answer including these best practices below](http://stackoverflow.com/a/21198129/151365). – logan Jan 17 '14 at 23:55
0

With jquery, either just replace the href with the correct one:

$('a').each(function() {
  this.href = this.href.replace(/all_products#/, 'products/');
});

or capture clicks and redirect:

$('a').click(function() {
  window.location = this.href.replace(/all_products#/, 'products/');
  return false;
});
kkyy
  • 12,214
  • 3
  • 32
  • 27
-1

this might help:

<a href="/all_products#A" onclick="return redirectMe(this);">A</a>
<a href="/all_products#B" onclick="return redirectMe(this);">B</a>
<a href="/all_products#C" onclick="return redirectMe(this);">C</a>
<a href="/all_products#D" onclick="return redirectMe(this);">D</a>
<a href="/all_products#E" onclick="return redirectMe(this);">E</a>

<script type="text/javascript">
function redirectMe(a){
   var aa=a+"";
   window.location=aa.replace(/#/g,"/");
}
</script>   
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • Downvote wasn't from me but this is not what I need. As kkyy's answer, this would only work for links on my own website, which I can change to the new schema anytime I want. I need to redirect links saved on OTHER websites over which I don't have control. – Tomas Andrle Aug 21 '09 at 20:03