New-Window Links in a Standards-Compliant World

annother nice thing about doing it this way is that you can still right-click > open in new window and you don’t get a stupid JS error, because it isn’t using JS to open the window… just change the target value.

i think puzanov is just missing the point here (like millions other… including browser coders…):
it’s not about “supporting” it’s about “conforming”! unless ALL THINGS conform, we’ll keep having to deal with 75934594 hacks for some browser redmond-based…

that’s the point in “Standards”…

Why on earth didn‘t they keep target in HTML? There should obviously be an easy way to open a link in another window.

And yes, I think the method provided in this article is too much of a hassle to be the standard in the future.

Why should it be in HTML? HTML describes document structure, not presentation or behavior. Presentation is described by CSS; behavior is described by Javascript.

window.open() is pretty easy :wink:

In comment to: “vgarcia Nov 1st, 2004 @ 11:47 AM MST”

When using Javascript’s “window.open()” the links will not work when javascript is unavailable; thus making it less accessible.

Really?


<a href="foo.html" onclick="window.open(this.href, 'blank', 'width=600,height=400'); return false;">Foo</a>

This link will work with JS disabled, it will just open in the same window instead of a new window. I think that’s pretty accessible. :wink:

The difficulty is with pop up blockers and window.open. The Google popup blocker stops my windows when using window.open.

How would I make this work with both A links and AREA links (from an imagemap)?

Actually, “target” can be used in XHTML1.1 because of it’s modular capabilities. It’s an element of the “target” module.
More info:
http://www.juicystudio.com/tutorial/xhtml/module.asp

What if you want to add multiple targets? How about blank1, blank2, blank3 for example.

How can this work with a button instead of a link?

Personally it annoys me if links are opened in new windows without letting me choose.

I did however take a poll on one of my sites and an overwhelming percent voted for off-site links opening in a new window. The best way I found for both preferences so far is this script.

If, like me, you prefer it to remember the visitors preference in a cookie so they don’t have to unclick the checkbox for each page, Dynamic Drive made an edited one that does just that :slight_smile: It also lets you choose which links will be effected by the script, so not all links will open a new window.

That last script is what I’ve added to my site, and so far visitor response has been positive. While it may not, in spirit, validate as strict (and as I understand it neither will the Sitepoint one if you get technical about it) it passes the validator and works with every browser I’ve tested it on.

Worth a look at really, gives the best of both sides in my opinion.

goole lists 1,330,000 i you search for _blank Seems like this is a web coders full employment program kind of thing, like they miss the Y2K revenue…

While this is a easy & useful workaround, there’s 1 underlying problem: What if the user has disabled Javascript?
You’ve effectively disabled opening links in a new window without explicit consent of the user (right click > open in new window).

Now, most people will agree this is a good thing, I’ll leave it up to the masses to decide.
I just don’t like being reliant upon Javascript for stuff like this.

Paranoids disable JavaScript for a good enough reason, but since XHTML/CSS2 does not do everything JavaScript does, web page designers need JavaScript, since getting strict about XHTML has so many on the learning curve. So, in order to filter through the validators, one needs to be shrewd. Thank you Sitepoint for the code.

hey, wouldn’t it be nice if the gurus who dictate what is “accessible” & what isn’t actually gave you an alternative?. It’s like being told “sorry, there’s no more fuel for your vehicles”, “we suggest you find a new way to propel your veicle.” um… thanks…
Not everyone likes to right click to open a browser in a new window… ahhhh well.
nice workaround though.

That is silly. “It validates”, but it sill uses the same deprecated attribute.

You’re fooling validator very well, but I doubt that requiring authors to use scripting workaounds was the intention behind removal of target attribute.

IAWporneL, it validates because validators don’t parse and execute javascript. The ‘rendered’ source will still have the same invalid attribute.

Despite that, using the ‘rel’ attribute is a good idea. Perhaps handling an onclick event from elements with the matching rel attribute would be more appropriate? AFAIK, window.open() emulates a _blank target.

Bit of a newbie question here.

Someone has asked me to build a site for them where certain internal links open in a new browser window but set to a smaller (but maximisable) size. Now I know I can do this with javascript using window.open() etc but this will be classed as a popup won’t it? In which case any user with a pop-up blocker installed won’t see the page.

So I guess my question is, is there any non-javascript way of getting an internal link to open in a smaller sized window?

Thanks for any help.

If you want to control the size of the window, you need to use window.open. If you use the onclick event most blockers let it through.

<a href="http://example.org/" class="external">example.org</a>
window.onload = function(){
 var a = document.getElementsByTagName("a");
 for(var i = 0; i < a.length; i++){
  if(a[i].className.indexOf("external") != -1 && a[i].href){
   a[i].onclick = function(){
    window.open(this.href, null, "width=640, height=480");
    return false;
   };
   a[i].title += a[i].title ? " (Opens in a new window)" : "Opens in a new window";
  };
 };
};