Overriding target attribute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmcgranahan
    New Member
    • Feb 2008
    • 1

    Overriding target attribute

    First, a disclaimer: I am a librarian by trade, but have the responsibility of maintaining/updating our online catalog. I've dealt with some JavaScript before, but I am no expert, so please forgive my ignorance.

    Question: Our automation system creates information dynamically for us. In one particular area of our page, it is adding a target=_self attribute that I believe is causing problems with our attempt at making a tab display. My theory is that if I can get this attribute changed to _blank, it might work, but I haven't figured out a way to do this. I have tried the following:

    [CODE=javascript]<script type="text/javascript">
    / * Checks for target attribute in link tags. */
    var f=window.docume nt.links;
    var i=f.length
    while(i--){
    f[i].target='_blank '
    }
    </script>
    [/CODE](the above was modified from another posting on this forum)

    However, this does not seem to work because the link is still going to the _self target. Any suggestions would be greatly appreciated. Thank you!

    Jamen McGranahan
    Last edited by acoder; Feb 14 '08, 10:50 AM. Reason: Added code tags
  • tswaters
    New Member
    • Feb 2007
    • 19

    #2
    Originally posted by jmcgranahan
    please forgive my ignorance.
    Forgiven!

    Originally posted by jmcgranahan
    Question: Our automation system creates information dynamically for us.
    You're talking server-side scripting -- like ASP or PHP? Or is this more of like a batch program to roll up a database into static html files?

    Originally posted by jmcgranahan
    In one particular area of our page, it is adding a target=_self attribute that I believe is causing problems with our attempt at making a tab display.
    Please elaborate on "tab display"

    Originally posted by jmcgranahan
    My theory is that if I can get this attribute changed to _blank, it might work,
    target is generally used with framesets -- if you have multiple frames, setting the "target" on an anchor will make the link open up in that frame... _blank usually means new page, _top means at the top level, above all the frames ... and most anything else -- i think, not sure -- if not found, the browser will open a new window and reference it by that name.

    "target" has been depricated in html4.01 strict.

    Originally posted by jmcgranahan
    but I haven't figured out a way to do this. I have tried the following:
    [code=javascript]
    / * Checks for target attribute in link tags. */
    var f=window.docume nt.links;
    var i=f.length
    while(i--){
    f[i].target='_blank '
    }
    [/code]
    *winces* -- that'll set 'em all... or raise an error... or something... try this:

    [html]<body onLoad="messWit hAnchors( '_blank', '_self' )">[/html]
    [code=javascript]
    function messWithAnchors ( _incomingTarget , _outgoingTarget ) {
    var a = document.getEle mentsByTagName( "A");
    for(x=0;x<a.len gth;x++) {
    if ( a[x].getAttribute( "target") == _incomingTarget ) {
    a[x].setAttribute(" target", _outgoingTarget );
    }

    // test function to verify the target was set properly
    // a[x].onclick = function() { alert(this.targ et); }
    }
    }
    [/code]

    but anyway... back to my first question, if this is being generated automatically somehow, it would be WAY easier and more efficient to change the root source than it would be to wait for the document to load and change all the anchors' targets via javascript.

    Comment

    Working...