dom – Convert JavaScript links to standard links – Education Career Blog

I am working with HTML which has javascript links like below:

<a href="javascript:openExternalLink(5542, true, '')">Links Text Here</a>

I need to replace these with standard anchor tags like so:

<a href="">Links Text Here</a>

What would be the best solution to achive this in Jython?




The best way would probably be to use regular expressions.


var i= 0, A= document.links, who, url;
    who= Ai++;
    url= who.href || '';
    if(url.indexOf('javascript:openExternalLink')== 0){
        who.href= url.substring(url.indexOf("'"), url.lastIndexOf("'")+1);


My solution is by using jQuery library (just for ease of use, you can simply do the code in pure Javascript by looping through anchors, because the rest of the code is pure Javascript).

Here you go, it loops through the anchors and sets the attribute href to the real one in DOM

$(document).ready(function () 
        $("a").each(function () 
            var href = $(this).attr('href');
            var urlStart = href.indexOf('http://'); //start point of the substring cut
            var urlStop = href.lastIndexOf("'"); //end point of the substring cut

            var realUrl = href.substring(urlStart, urlStop); //this is the real URL

            $(this).attr( {'href': realUrl} ); //now replace and we're ready to go

I tested this method myself and it works as intended. Enjoy!


Something like this might work:

newhtml = oldhtml.replace(/href=".*?'(http:.*?)'.*?"/gi, 'href="$1"');

Leave a Comment