467,146 Members | 1,265 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. / * Checks for target attribute in link tags. */
  3. var f=window.document.links;
  4. var i=f.length
  5. while(i--){
  6.      f[i].target='_blank'
  7. }
  8. </script>
  9.  
(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
Feb 13 '08 #1
  • viewed: 1411
Share:
1 Reply
please forgive my ignorance.
Forgiven!

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?

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"

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.

but I haven't figured out a way to do this. I have tried the following:
Expand|Select|Wrap|Line Numbers
  1. / * Checks for target attribute in link tags. */
  2. var f=window.document.links;
  3. var i=f.length
  4. while(i--){
  5.      f[i].target='_blank'
  6. }
  7.  
*winces* -- that'll set 'em all... or raise an error... or something... try this:

[html]<body onLoad="messWithAnchors( '_blank', '_self' )">[/html]
Expand|Select|Wrap|Line Numbers
  1. function messWithAnchors( _incomingTarget, _outgoingTarget ) {
  2.   var a = document.getElementsByTagName("A");
  3.   for(x=0;x<a.length;x++) {
  4.     if ( a[x].getAttribute( "target") == _incomingTarget ) {
  5.       a[x].setAttribute("target", _outgoingTarget );
  6.     }
  7.  
  8.     // test function to verify the target was set properly
  9.     // a[x].onclick = function() { alert(this.target); }
  10.   }
  11. }
  12.  
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.
Feb 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

24 posts views Thread by Mike | last post: by
16 posts views Thread by Mark van Ballegooijen | last post: by
36 posts views Thread by randau | last post: by
2 posts views Thread by ESPNSTI | last post: by
1 post views Thread by Joe | last post: by
4 posts views Thread by Raja Raman Sundararajan | last post: by
92 posts views Thread by Erwin Moller | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.