-1

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 ?

4
  • This query value is the query string of the current URL? Commented Dec 27, 2010 at 13:22
  • yes, basically I need to append something to third argument coming from query string. Data types can be ignored. Commented Dec 27, 2010 at 13:23
  • If you are using jQuery then why are you using javascript: RedirectLink("1", "2", "3" + param, "4" , "5") in your html? jQuery has specific event handlers that allow you to properly separate your concerns. What you have written is very poor practice. Commented Dec 27, 2010 at 14:09
  • Can you elaborate more? I am just constructing the link which I will assign to anchor. How else can it be done ? Commented Dec 27, 2010 at 14:10

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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;
    });

});

1 Comment

Thanks, This looks like a lot of parsing. Let me try this too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.