samedi 27 juin 2015

Link Specific Redirection Script

I have a below script, is it possible to add link specific redirect like -

  • http://ift.tt/1GDMx2N : http://ift.tt/1Ho2r81
  • www.Link2.com/z/x : http://ift.tt/1GDMx2P

I tried adding variables but unable to add subfolders in the link url to the redirect.

<a href="http://link1.com">link 1</a>
<a href="http://ift.tt/1TV4y6K">link 2</a>
<a href="http://wikipedia.org">not redirected</a>

<script>
var aTags = document.getElementsByTagName("a");
var redirects = {
    "http://link1.com": "http://google.com",
    "link2.com": "http://google.com"
};

for(var i = 0;i < aTags.length; i++) {
    var url = aTags[i].getAttribute("href");

    for (var redirect in redirects) {
        var pattern = new RegExp(redirect);

        if (pattern.test(url)) {
            aTags[i].setAttribute("href", redirects[redirect]);
        }
    }
}
</script>

Aucun commentaire:

Enregistrer un commentaire