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 ?
-
This query value is the query string of the current URL?Šime Vidas– Šime Vidas2010年12月27日 13:22:24 +00:00Commented 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.Madhur Ahuja– Madhur Ahuja2010年12月27日 13:23:29 +00:00Commented 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.James South– James South2010年12月27日 14:09:21 +00:00Commented 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 ?Madhur Ahuja– Madhur Ahuja2010年12月27日 14:10:35 +00:00Commented Dec 27, 2010 at 14:10
2 Answers 2
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).
Comments
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;
});
});