SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Yugo full of anvils bronze trophy hillsy's Avatar
    Join Date
    May 2001
    Location
    :noitacoL
    Posts
    1,859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Link rewriting code?

    I'm trying to write some JavaScript that will serach for links on a page with a certain hostname, and rewrite them so that they're pointing through a "link counting" script which then transfers the request onto the appropriate destination. Currently this is all hardcoded into the URL which is a bit of a pain (it's for an intranet CMS), making everything longer and more complicated than it needs to be. The idea is to use JS to automatically write the piece of the link (to the counter script) that's going to be the same for every one.

    However I'm not a JS guru by any stretch of the imagination (there's an understatement ) and I'm having a lot of trouble with document.links

    Firstly, what's wrong with this code? Whenever I try it, I get a "...hostname is null or not an object" error

    Code:
    // loop over the links and evaluate them
    for (i = 0; i <= document.links.length; i++) {
    	hostName = document.links[i].hostname;
    	if (hostName = 'localhost') {
    		currLink = document.links[i].href;
    		// code to rewrite the link will go here
    	}
    }
    Any ideas? This is before I even get to the code for rewriting the link itself (more on that later I guess...)
    that's me!
    Now A Pom. And a Plone Nut
    Broccoli Martinez Airpark

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    afaik, document.links has only 1 property -- length
    you can check properties with the following code:

    for (property in objectName)
    alert("The property " + property + " is defined as " + objectName.property);

    However, since document.links[i] supplies the address the href points to, you want the following (I think):

    var hostName = "";
    var curLink = "";
    for (var i = 0; i <= document.links.length; i++)
    {
    hostName = document.links[i];
    if (hostName == 'localhost')
    currLink = hostName;
    }

    note the use of single and double "=" signs.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    Santos L Halper Zenith's Avatar
    Join Date
    May 2002
    Location
    Finland
    Posts
    641
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    = means "placing": myvariable = 'something'
    == means "comparison": if(foo == bar)

    -Z-

  4. #4
    Yugo full of anvils bronze trophy hillsy's Avatar
    Join Date
    May 2001
    Location
    :noitacoL
    Posts
    1,859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes - good call on the ==

    I've been doing ColdFusion too long so I've gotten out of the "proper" way of doing assignments and comparisons

    Anyway, document.links should support the hostname property, according to MSDN:
    http://msdn.microsoft.com/library/de...mentobject.asp

    This is reminding me why I don't like programming Javascript. It's so inconsistently documented compared to most of the server-side platforms (notably PHP and CF, both of which have brilliant doco)

    Anyway, still trying...
    that's me!
    Now A Pom. And a Plone Nut
    Broccoli Martinez Airpark

  5. #5
    Yugo full of anvils bronze trophy hillsy's Avatar
    Join Date
    May 2001
    Location
    :noitacoL
    Posts
    1,859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working [img]images/smilies/biggrin.gif[/img] And despite the fact that all the doco says that document.links is read-only, there seems to be no problem in practice with over-writing the value.

    The issue was in fact that I was using <= in the for () statement. I'm guessing the problem was that, in iteration of the loop, document.links.length cannot be zero if it's also going to have hostname etc. By removing the zero equivalency (i.e. just using < ) it worked fine.

    For posterity, here it is:
    PHP Code:
    // IP address display using ColdFusion Remote User variable
    var iptUser '#iptUser#'

    // path to the link tracking script
    // this script must be coded to accept the parameters targetURL and iptUser
    var trkScript 'http://path/to/script.cfm'

    // hostname you want to check for
    var chkHost 'hostname'

    // loop over the links and evaluate them
    function chkDomLinks () {
    for (
    0document.links.lengthi++) {
    hostName document.links[i].hostname;
    currLink escape(document.links[i].href); //account for special url chars 
    newLink trkScript "?targetURL=" currLink "&iptUser=" iptUser;
    if (
    hostName == chkHost) {
    document.links[i].href newLink;
    }
    }

    Last edited by hillsy; Jul 29, 2003 at 12:25.
    that's me!
    Now A Pom. And a Plone Nut
    Broccoli Martinez Airpark


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •