Prepend all links that are not intra-site with a string in JS... Can you just proofread this quick little script I hacked together?

Go To StackoverFlow.com

0

The idea is to route the outgoing links through DDG's redirect service, to conceal my site in referrers. Is there anything wrong with the script as it is?

Also, see the comment in the code... Is the mentioned alternative superior? Inferior? Identical?

Thanks again!

var baseUrl='https://duckduckgo.com/l/?u=';

var invisibleHost=window.location.hostname;

var pageLinks=document.getElementsByTagName('a');
var n_links=pageLinks.length;

var leaveAsIs=true;

for(var i=0;i<n_links;i++){

    var presentLink=pageLinks[i].href;
    leaveAsIs=true;

    if(/^https?:\/\//i.test(presentLink) && invisibleHost.test(presentLink))
        leaveAsIs=false;

    pageLinks[i].href=leaveAsIs?presentLink:baseUrl+presentLink;
}
2012-04-03 20:48
by user1311065
Might want to post this on http://codereview.stackexchange.com/ - j08691 2012-04-03 20:58


0

always use test() unless you need to return, rather than simply test for, a match.

Also, your nested if conditions could be one, multi-condition, and you don't need two REGEXs for http and https - just use one but stipulate that the 's' is optional:

/^https?:\/\//i.test(presentLink)
2012-04-03 20:55
by NoName
>
  • so the colon doesn't need to be escaped?
  • - user1311065 2012-04-03 20:59
    how's that? (see edited main question body - user1311065 2012-04-03 21:02
    No - colons are not special characters in JS REGEXP (except when used as part of the special ?: flag for stipulating non-captured sub-matches) - NoName 2012-04-03 21:04
    Yeap, looks OK - NoName 2012-04-03 21:04
    Ads