Javascript - Dynamic Rewrite of All External Links on a Page?

Discussion in 'JavaScript' started by ilikenwf, Jun 4, 2009.

  1. #1
    I'm first off very bad at java. If it were PHP or any other object-oriented language I'd be fine. That said, I really would appreciate it if someone could help me come up with a javascript that will dynamically rewrite only the external links on every page I put the java into the header of. This is so I can redirect them to a redirector, in order to use an iframe script I have to try and get them to return to my site.

    Any chance someone could help a guy out with this? I really couldn't find it through google or hotscripts, otherwise I wouldn't be asking. (And I'd write it if I could, but I just never have been able to handle java as well as php or c, or even bash...lol).
     
    ilikenwf, Jun 4, 2009 IP
  2. Straightupimp

    Straightupimp Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Java & JavaScript Are Two Very Different Things, JavaScript Is Actually Similar To PHP; So If You Know PHP You Should Be Fine
     
    Straightupimp, Jun 4, 2009 IP
  3. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yep, the only thing Java and javascript have in common is just the name. About the second part - javascript is not at all similar to PHP. Whatever.. this isn't the point.

    Generally, to do what you want you can use document.getElementsByTagName() . Get all <a> elements on the page and then rewrite their href attribute.
     
    xlcho, Jun 4, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    You could just as well do this in a PHP-function - for instance, check all HREF-attribures, and if they're not part of your domain (ie. external links) rewrite the links to whatever you want. Shouldn't be too hard implementing a parsing function with str_replace or preg_replace for whatever content you show on your website. This would also mean that it would work regardless of whether the user have javascript turned on or not.
     
    PoPSiCLe, Jun 4, 2009 IP
  5. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Huh?

    PHP isn't an OOP language.

    JavaScript & Java are both OOP languages.
     
    MMJ, Jun 4, 2009 IP