Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery link redirect

I am writing an asp.net c# webforms website

I am trying to write some jquery to achieve the following:

When the below link is clicked

<a title="Relevance" href="www.url.com/search/1/3/5/1/4/0">my link</a>

I would like to have some jquery that takes the href and replaces the last number in this case 0 altho we can change the 0 for any replacable string it this makes it easier. with the value of an once this has been done we would like to then redirect the user to the new URL.

Is this easily achievable in jquery? Can any one point me in the direction of a tutorial to achieve anything similar to this?


1 Answers

Yes, you can do something like this (not checked for accuracy, but the algorithm should be sound):

$('#myLink').click(function()
{
     var currentUrl = this.href;

     var items = currentUrl.split('/');
     items[items.length - 1] = 'myNewValue';


     window.location.href = items.join('/');
});

Effectively, split by the slash, then replace the last element, rejoin the string together, and set the window's href to your new location.

like image 82
Tejs Avatar answered Feb 04 '26 07:02

Tejs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!