Jquery redirects

I am writing an asp.net c # webforms website

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

Clicking the following link

<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 href and replaces the last number in this case 0 altho we can change 0 for any string being replaced, this makes it easier. with a value of once when this was done, we would like to redirect the user to a new URL.

Is this easy to achieve in jquery? Can someone point me towards a textbook to achieve something like this?

+3
source share
4 answers

0 , String.Empty? , - URL-, .

, :

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

, , :

$('a.search-link').click(function()
{
     var currentUrl = this.href;             
     currentUrl += 'myNewValue';        
     window.location.href = currentUrl;
});
+2

, - ( , ):

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

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


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

, , , href .

+4

: http://jsfiddle.net/9Kvfh/

$(function()
{
    $('.replaceLastNumber').each(function()
    {
        $(this).click(function(ev)
        {
            ev.preventDefault();

            var ahref = $(this).attr('href');
            var Length = ahref.length;

            if (ahref.slice(Length - 1, Length) == '/') //Remove trailing slash
                ahref = ahref.slice(0, Length - 2);
            else
                ahref = ahref.slice(0, Length - 1);
            ahref += "SomeNewValue";

            window.location.href = ahref;
        });
    });
});

, - ( , , PHP-, js off).

0

, , , :

HTML:

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

JQuery

$('#link1').click(function(){

    // get the url
    var url = this.href;

    // get the length of the final slash value
    var finalSlashLength = url.substring(url.lastIndexOf('/') + 1).length;

    // trim the final value off
    var newUrl = url.substring(0, url.length - finalSlashLength);

    // add the new value
    newUrl += 'some new value';

    // redirect to the new URL
    window.location.href = newUrl;     
});

:

http://jsfiddle.net/QRpYC/2/

EDIT:

As you mention in the comments, the markup is generated dynamically - you can easily find links based on other parameters. The first thing I would be looking for is to sit in a shared container, like div, etc.? Find the parent with the name, class, or identifier attribute and use it as a selector.

For example, if the markup looks like this:

<div id="searchLinks">
    <a title="Relevance" href="http://www.url.com/search/1/3/5/1/4/0">my link</a>
    <a title="SomeLink" href="http://www.url.com/search/1/3/5/1/4/1">my link</a>
</div>

Then you can select them using:

$('div#searchLinks > a').click(function(){....});
0
source

All Articles