Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

target ="_blank"

Status
Not open for further replies.

jakeyg

Programmer
Mar 2, 2005
517
GB
As
target ="_blank"
isn't supported in proper web standards, what's the recommended method for opening a new browser window ?
ta
 
If you must, use javascript's window.open. Since websites are supposed to show content and not meddle with your browser, target="_blank" was removed and only JS (which is used to provide client side scripting) solution remains.
 
where is documentation describing the deprecation of target? it's still listed on w3, what am i missing?



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Cory,

It fails if you use the strict doctype doesn't it? e.g
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
   <head>
      <title>Anchor Test</title>
   </head>
   <body>
      <div><a id="myAnchor" target="_blank" href="[URL unfurl="true"]http://www.google.com">Google</a></div>[/URL]
   </body>
</html>


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Yeah, I know I've seen it somewhere but I couldn't find it from google either so I just ran that quick example through and it failed so it must be documented somewhere.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
From a comment here:


That Post said:
Target isn't deprecated but not defined in HTML 4.01 Strict DTD, XHTML 1.0 Strict DTD and XHTML 1.1 DTD.

Without use of frames target attribute should be disallowed.

Interesting comment about frames - I can see why a Frameset DTD would still have this included.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
It *is* just the strict doctype that won't allow it

I decided not to use javascript when I found that method as I aren't really a fan of JS anyway and most browsers go nuts about active content if you include it.

I've got a load of photographs which, in my old site, I popped up in their own window, as people like to compare and contrast them and they're not very techno and opening up a new browser manually...well it confused them, they like to do as little work as possible.

My links page pops up a new window too because as I've read in the past it helps to seperate the sites in the users mind and keeps your site so the user knows how they got there and how marvelous you are :) Has this advice/guidelines changed too now?

And i've got an emoticons pop up like here in Tek tips, just cause I like it :p

So basically it's tough then! I'm either happy of user and the wc3 hate me or vice versa :-(
 
You could always put the photos in a separate div on the page and then change the innerHTML to display the appropriate picture.

Lee
 
Popping up the photos in a new window probably does make sense, but you should consider using Javascript for this since it will enable you to do things like suppress the address and status bars, and size the window to fit your pictures.

Pop-up help / pick lists of manky emoticons is another valid case, but personally I'd reach for JS again. If you really don't want this, just use a Transitional or Frameset doctype and stick with [tt]target[/tt].

Opening external links in a new window is just foolish. It always was and it still is. The justification often offered for it is that "it stops people leaving your site". If I want to leave your site, I'm gonna leave your site. Excessive opening of new windows just means I'm not gonna come back to it...

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Here is how I would do the code to allow me to show thumbnails of the images and also allow them to be clicked. This solution is accessible - in that it degrades without javascript and doesn't lose functionality. It basically combines many of the suggestions here - and a little crafting...
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>Test Lightbox Harness</title>

<script type="text/javascript">
/* this function displays the large image in a javascript popup window */
function showImage(_data) {
  var myHdl = window.open(_data,'popupwindowname','width=400,height=600');
}

/* this function is run once the page has loaded and attaches the showImage()
   function to each <href> inside the <div> with a class of "lightbox" */
function initLightbox() {
  var divCollection = document.getElementsByTagName('div');
  for (var loop1=0,max1=divCollection.length; loop1 < max1; loop1++) {
    if (divCollection[loop1].className.indexOf('lightbox') != -1) {
      var hrefCollection = divCollection[loop1].getElementsByTagName('a');
      for (var loop2=0, max2=hrefCollection.length; loop2<max2; loop2++) {
        hrefCollection[loop2].onclick = function() { showImage(this.href); return false; };
      }
    }
  }
}
window.onload = initLightbox;
</script>
</head>

<body>
<div class="lightbox">
  <div>
    <a href="images/picture1Big.jpg" target="_blank">
      <img src="images/picture1Thumb.jpg" width="150" height="300" alt="Picture description 1" />
    </a>
  </div>
  <div>
    <a href="images/picture2Big.jpg" target="_blank">
      <img src="images/picture2Thumb.jpg" width="150" height="300" alt="Picture description 2" />
    </a>
  </div>
  <div>
    <a href="images/picture2Big.jpg" target="_blank">
      <img src="images/picture2Thumb.jpg" width="150" height="300" alt="Picture description 3" />
    </a>
  </div>
</div>
</body>
</html>
Note that I have opted for HTML 4.01 Transitional doctype to allow me to use the target attribute in the <href> tags. The page harness above fully validates.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Here a quick, easy, and flexible option I have found to solve this problem: Use the following JS in the anchor tag.

Code:
<a onclick="javascript:this.target='_blank'" href="...">

The following example tests OK in HTML Strict, and it also works in Transitional and quirks mode. It will not affect code validation.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<head>
<title>Target=_blank in HTML 4.01 Strict</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<a onclick="javascript:this.target='_blank'" href="[URL unfurl="true"]http://www.tek-tips.com/">Click[/URL] here</a> to open a new window.
</body>
</html>

If JS is critical to your page's functionality, then it should make no difference. If the user has JS turned off, then all the JS in the onclick behavior will simply be ignored and the anchor will default as 'target="_self"' (i.e. open in the same window).

I think this would be an example of functionality that "fails gracefully".
 
Kind of similar, but more "desirable" since it doesn't use a psuedo protocol (i.e javascript:)
Code:
<a href="[URL unfurl="true"]www.anothersite.com"[/URL] onclick="window.open(this.href); return false;">Look at this great site in another window!</a>

If javascript is enabled the page will open in a new window. If javascript is disabled then the link will simply open as normal.

<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Buy Languedoc wines in the UK
 
Interesting, Foamcow. I'm assuming you mean that "version 4" browsers have trouble with the pseudo protocol?

Craig
 
So right about "javascript:"!
I did a little more research into the logic of avoiding pseudo protocols, and it seems that they...
* disable the normal bookmarking process, including "open in tabs".
* disable link copying.
* are not accessible to web crawlers.
* adversely affect search engine optimization.
* break on high security settings.
* And, of course, are not accessible to script-disabled environments.

Yet I wonder if any of these deleterious effects apply equally to a scripted link behavior even without the pseudo protocol.

Perhaps the "target=_blank" behavior is on its way out (as a standards-based behavior) until CSS/browsers get more behavior-setting power.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top