SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy assign target to nested links only?

    I'm looking for a way to use javascript to assign target="_blank" via javascript to all links that appear within a <td> with id="cms.
    All other links should be left untouched (so they'll use the default _self target).


    Code:
    function blankLinks() {
    var o = ( document.all ) ? event.srcElement : e.target;
    if ( o.nodeName != 'A' ) return;
    o.target = "_blank";
    }
    Beetle kindly supplied the above script for me as a response to my post over at this thread.
    (specifically this post)

    Even though my understanding of javascript is still fairly limited I still don't recognise anything in that script that relates to the <td>s id, or rather, where the <td>s id might fit in.

    So, I'm hoping someone can step in and either complete the script or explain to me how it could be implemented as it is.


    Any input is greatly appreciated, so many thanks in advance.
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  2. #2
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Beetle's code is a excellent way to implement a simple DOM solution. All you need to do to make it work is put an onclick calling the function on the td's that you want to target as blank:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function blankLinks() {
    var o = ( document.all ) ? event.srcElement : e.target;
    if ( o.nodeName != 'A' ) return;
    o.target = "_blank";
    }
    
    
    </script>
    </head>
    
    <body>
    	<table cellpadding="10">
    	<tr>
    		<th>&lt;td onclick="blankLinks();"&gt;</th><th>no onclick</th>
    	</tr>
    	 <td onclick="blankLinks();">
    		<a href="http://www.google.com/">new window</a><br>
    		<a href="http://www.xdevdesign.com/">new window</a>
    	 </td>
    	 <td>
    	 	<a href="http://www.xdevdesign.com/">same window</a>
    	 </td>
    	</tr>
    	</table>
    	<p>not in a table: <a href="http://www.codingforums.com/">same window</a></p>
    </body>
    </html>
    When you click anything in the td then the script looks at what the target element was (event.srcElement(IE) || e.target(NS)), if the nodeName is not an anchor tag you return nothing, if it is, you set it to blank.

    Hope this helps,
    xDev

  3. #3
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I understand it now.

    I was looking at it thinking it was an automated script in the same way that Yank's 'New Window in a Standard Compliant World' script method is.
    I was expecting something that would automatically preassign the target attribute to certain links onload.

    Now I know what angle this script is coming from, I can see it more clearly.

    Looking at it, one thing occurs to me...

    How are non-IE browsers supposed to know what the e var is when it hasn't been defined?

    Surely that's gonna cause the script to fail and throw an error in non-IE browsers, no?.

    Edit:



    Checked and non-IE browser do indeed fail the script.
    Tested on Moz (Et al) and Opera.

    So, what now?

    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  4. #4
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm.. your right. I didn't test in NS or Opera just figured it would work. Netscape's e.target is probably conflicting with the already named links.target attribute. I'll have to think about a different way of doing of solving your problem.

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here, do it this way
    Code:
    function blankLinks( evt )
    {
    var o = ( evt.target ) ? evt.target: event.srcElement;
    if ( o.nodeName != 'A' ) return;
    o.target = "_blank";
    }
    And then
    Code:
    <td onclick="blankLinks( event );">
    *not tested* but should work.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *does* work

    *very* grateful




    (Beetle, IOU some rep points. Gotta 'spread 'em around' a bit first.)



    [aah]

    No go in Opera.

    Would this be down to the method used inthe script or due to Opera's renowned lack of DHTML/DOM support?

    If it's due to the method, is there some way to script it to get Opera on board too?

    If it's a failing of Opera then I'm sure having links open in _self is a burden that can be lived with.

    [/aah]
    Last edited by Bill Posters; Jul 7, 2003 at 07:53.
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature


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
  •