-2

The following script adds my affiliate extension to every url on my page. For example, if http://www.Google.com/ is on my page somewhere... Adding this script to the page will automatically change the url to- http://adf.ly/xxxxxx/http://www.Google.com/ - this goes for every url on the page...

This script works great if the urls on the page are already hyperlinked, because the link text stays the same, but the destination url now has my affiliate extension on it.

My question is... How could I modify this script to make it so that raw urls (not hyperlinked) can still be shown as the original url, but with my affiliate extension added to the destination? Better put, I need this script to hyperlink all of the raw urls on the page. I need it so that if I have the raw url http://www.Google.com on my page, the script will change the destination to- http://adf.ly/xxxxxx/http://www.Google.com/ - but the viewer will still only see http://www.Google.com.

Again, this script works fine with hyperlinks already as it only affects the URLs, not the link text. I just need it to cloak/mask/hyperlink all of the raw urls, so that viewers still see the original URL destination, rather than the new one with my affiliate extension on it.

Thank you in advance, Here is the example HTML code:

<script type="text/javascript">
    onmousemove = function adfly() {
        adfly_id = 'xxxxxx';
        for(var i = 0; i < document.links.length; i++) {
            var hrefer = document.links[i].href;
            if(hrefer.match("adf.ly") || hrefer.match("javascript:") || hrefer.match("#")) {
                document.links[i].href = document.links[i].href;
            } else {
                document.links[i].href = 'http://adf.ly/' + adfly_id + '/' + document.links[i].href;
            }
        }
    }
</script>
Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Thomas
  • 1
  • 1
  • 2
  • 1
    I'd be more inclined to help you if it was a script to remove those annoying cash-makers. :) Anyway, what are you trying to do exactly? Whenever the user mouseover the link, he will always see the `adf.ly` address in the status bar (unless you do some masking through JS `function`/`var`). I don't see your script changing the links' display text either. – Fabrício Matté Jun 04 '12 at 00:53
  • It's fine that the user will see the adf.ly destination if they mouseover, that's not a concern of mine. What I need to accomplish is for raw urls to become hyperlinks. The hyperlink text being the original URL, and the hyperlink destination being the new affiliate extension URL. I know this could easily be accomplished by manually switching all of my raw urls to hyperlinks, but I need this to be done automatically. And Fabricio, how could you modify the script with fuction/var to cloak the URL on mouseover? – Thomas Jun 04 '12 at 00:57
  • Edit your question to include a little more code and some examples for testing. In your code example, I only see the script changing the `href` attribute and not the link's display text. – Fabrício Matté Jun 04 '12 at 01:44
  • That is what I'm asking for Fabricio! I don't know how to make this script change the text... I don't want it to just add the affiliate extension to the raw url like it is currently doing... I need it to change the raw url to a hyperlink where the original url is used as the text and the destination in the new affiliate url. – Thomas Jun 04 '12 at 01:54

2 Answers2

0

You could do it by:

  1. Going through text nodes (or the subset you are interested in), finding ones which contain a match for a URL regular expression.
  2. Create new nodes for:
    • the text parts
    • the anchor/links
  3. Insert these new nodes into the DOM (deleting/replacing the original text node)

This would be a loop separate to your existing one that goes through document.links.

Last but not least: be wary of XSS vulnurabilities!

Community
  • 1
  • 1
chees
  • 605
  • 4
  • 11
-2

The anchor tag below shows "google.fr" in the href attribute, but because of the onclick, clicking the link will take you to the site listed there instead.

 <a title="google" href="http://google.fr/whatpeopleseeonrollover" onclick="this.href='http://yahoo.fr/wherepeoplegoforreal'">http://google.fr/whatpeoplethinktheygo</a>

You could use something like this to redirect links on your page.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Maltazar
  • 45
  • 2
  • 8
  • 1
    What is posted here is only the start of an idea for a solution, and not a full answer. Yes, this particular trick works, but you should flush out the details on how to implement it into the OP's problem. – gunr2171 Oct 01 '15 at 18:05