Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery rewrite href

I have an onclick function that I want to add an anchor to the href value. I do not want to change the URL's, because I need the site to still function for people without javascript / for SEO purposes. so here is what I have tried using (amoung other things):

jQuery('a[rel=ajax]').click(function () {
    jQuery(this).attr('href', '/#' + jQuery('a'));
});

An orginal link looks like this:

http://www.mysite.com/PopularTags/

URL re-write should look like this, so that AJAX will work:

http://www.mysite.com/#PopularTags

I was able to get some URL's to work by setting a links name value to the same as the href, but it did not work for links with sub-sections:

http://www.mysite.com/Artist/BandName/

So not really sure. Thanks for the help.

like image 221
brandon14_99 Avatar asked Apr 15 '10 06:04

brandon14_99


1 Answers

I'd suggest to use regular expressions, like

$('a[rel=ajax]').click(function(){
  $(this).attr('href', function(){
    this.href = this.href.replace(/\/$/, "");
    return(this.href.replace(/(.*\/)/, "$1#"));
  });
});
like image 123
jAndy Avatar answered Sep 22 '22 10:09

jAndy