views:

266

answers:

4

Hey,

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

<a href="javascript:openExternalLink(5542, true, 'http://www.websitedomain.com')"&gt;Links Text Here</a>

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

<a href="http://www.websitedomain.com"&gt;Links Text Here</a>

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

Thanks

Eef

+1  A: 

The best way would probably be to use regular expressions.

Darth
A: 

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!

Bogdan Constantinescu
Although this is a nice solution, I think it is only of limited functionality. One important reason to avoid javascript: links is to support non-javascript browsers. Obviously your solution won't do this.
Joachim Sauer
I would not be able to do this as I can not run JavaScript over the content I am only limited to Jython :/
Eef
I'm sure part of his purpose is SEO, and this certainly is not going to help with that.
Josh Stodola
+1  A: 
var i= 0, A= document.links, who, url;
while(A[i]){
    who= A[i++];
    url= who.href || '';
    if(url.indexOf('javascript:openExternalLink')== 0){
     who.href= url.substring(url.indexOf("'"), url.lastIndexOf("'")+1);
    }
}
kennebec
A: 

Something like this might work:

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