SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Why is this link opening code wrong?

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    London
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why is this link opening code wrong?

    I'm trying to write a cold which takes all the links in the page (within the content div) and sets their onclick to open in a new window...

    I can't see why it wouldn't work...

    Any takers???

    Code:
    function linkBehaviour() {
    	for (var j=0; j<document.getElementById(content).anchor.length; j++) {
    	document.anchors[j].onclick = window.open(this.href);
    		return true;
    		}
    }

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A couple problems...

    1) I don't think .anchor gives you back a list of anchor elements that are children of that div, but I haven't tried that

    2) document.anchors is not the same array as document.getElementById(content).anchor, so you can't index into it in that order. You're editing arbitrary links in the document, regardless of whether they're within your #content div or not.

    3) I think you want document.anchors[j].href, not this.href, to reference the href of the anchor you're changing the onclick value of in that loop
    17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
    Conversion tracking, click fraud detection, A/B testing and more.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    London
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    Thanks for your reply...I'm going to re-edit using what you've said, and repost when I have something...

    Jim

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
  •