views:

514

answers:

4

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.

A: 

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
down vote and no comment?
TheVillageIdiot
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.
TomA
+1  A: 

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
But this would only work for links on my site. Not for existing links from other sites. Right?
TomA
+1  A: 

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
Please note: There is an issue with using this within a "default" page in a directory for ie8: It loses the hash value.
Gregory
+1  A: 

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