change anchor href with jquery

327 Views Asked by At

I need to iterate through all anchors on a page with specific hrefs and replace the href contents using jquery.

I need this:

<a onfocus="OnLink(this)" href='javascript:CoreInvoke("MtgNavigate","20131211");' target="_self"><u>12/11/2013</u></a>

to be replaced with this:

<a onfocus="OnLink(this)" href='javascript:MtgNavigate("20131211");' target="_self"><u>12/11/2013</u></a>

Basically, I want to replace CoreInvoke with MtgNavigate and remove the MtgNavigate as a parameter.

Each link though will have a different number in the second parameter of the original link. Any ideas how I can do this?

2

There are 2 best solutions below

0
On BEST ANSWER

Check this fiddle.

$(document).ready(function(){
    $("a").each(function(){
        var token = $(this).attr("href");
        token = token.substring(token.indexOf(',"')+2, token.indexOf('")'));
        $(this).attr("href","javascript:MtgNavigate(\""+token+"\");");
    });
});

The fiddle will execute on page ready. If you need to do this after any event means create a function and trigger on the event execution.

0
On

Using jquery it would go something like:

$('a').each(function(){
    var href=$(this).attr('href');
    if(href.indexOf('javascript:CoreInvoke')==0)
        $(this).attr('href','javascript:' + href.substring(href.indexOf('("')+2, href.indexOf('","')) + '(' + href.substring(href.indexOf('","')+2));
});