Madhur Ahuja
Madhur Ahuja

Reputation: 22661

Help with Jquery function

Ok, Here is my problem:

I have this function in a third party javascript library defined as RedirectLink(a,b,c,d,e) where a,b,c,d,e are strings.

I also have a link in my webSite which is again autogenerated (And I can't change it)

<a href="javascript: RedirectLink("1", "2", "3" ,"4" ,"5")" id="mylink" >

What I need to do is, read this anchor's (mylink) href and I need to call RedirectLink function such as javascript: RedirectLink("1", "2", "3" + param, "4" , "5"). where param will come from query string.

The first part is easy, reading the href but how to alter the third parameter ?

Upvotes: 0

Views: 65

Answers (2)

Šime Vidas
Šime Vidas

Reputation: 185913

This would work:

$(function() {

    var $mylink = $('#mylink');
    var href = $mylink.attr('href');

    // getting the array of arguments
    var args = href.substr( href.indexOf('(') + 1 );
    args = args.slice(0, args.length - 1);
    args = args.split(',');

    // getting the string value for each array item
    for (var i = 0; i < args.length; i++) {
        args[i] = $.trim(args[i]);
        args[i] = args[i].slice(1, args[i].length - 1);
    }

    // augmenting the third argument
    args[2] += window.location.href;

    $mylink.attr('href', '#').click(function() {
        RedirectLink(args[0], args[1], args[2], args[3], args[4]);
        return false;
    });

});

Upvotes: 1

Don
Don

Reputation: 17606

Mhm... You can make an hack like this:

via javascript you can change href from javascript: RedirectLink(1,2,3,4,5) to javascript: MyRedirectLink(1,2,3,4,5), then define MyRedirectLink:

var param = 'xxx';
function MyRedirectLink(a, b, c, d, e) {
    RedirectLink(a, b, c + param, d, e)
}

Not elegant but it should work (if param can be computed outside your function).

Upvotes: 1

Related Questions