Jump to content

how to <a target="_blank"> in XHTML 1.1?


midnite
 Share

Recommended Posts

You can use the target= , but it won't be valid, or use a javascript on-click event handler to add the target= attribute, but it would still be opening a new window, so it also violates the principle of the standards, except the validator does not 'see' the target= if it is done with js.

Link to comment
Share on other sites

another possibility which I think is a bit better because this takes popup blockers into account. If the the window fails to open because of a popup blocker the link will still open in the parent window.

<a href="http://www.google.com" onclick="return !window.open(this.href)">Google It!</a>

Link to comment
Share on other sites

maybe the following is a another way:

<script>function externallinks() {  if (!document.getElementsByTagName) return;  var anchors = document.getElementsByTagName("a");  for (var i=0; i<anchors.length; i++) {    var anchor = anchors[i];    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 	 anchor.target = "_blank";  } } window.onload = externallinks;<script><a href="document.html" rel="external">open a new window</a>

Link to comment
Share on other sites

maybe the following is a another way:
<script>function externallinks() {  if (!document.getElementsByTagName) return;  var anchors = document.getElementsByTagName("a");  for (var i=0; i<anchors.length; i++) {    var anchor = anchors[i];    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 	 anchor.target = "_blank";  } } window.onload = externallinks;<script><a href="document.html" rel="external">open a new window</a>

That is how I prefer to do it too, but for some strage reason, it doesn't work for me in IE when using XHTML 1.1. It does work on XHTML 1.0 Strict though.What surprises me more is that it works in Firefox and Opera, even though I serve XHTML as applicaiton/xhtml+xml for them.
Link to comment
Share on other sites

That is how I prefer to do it too, but for some strage reason, it doesn't work for me in IE when using XHTML 1.1. It does work on XHTML 1.0 Strict though.What surprises me more is that it works in Firefox and Opera, even though I serve XHTML as applicaiton/xhtml+xml for them.
is it because the problem that IE runs window.onload = externallinks; before everything is really loaded, especially the DOM tree?
Link to comment
Share on other sites

is it because the problem that IE runs window.onload = externallinks; before everything is really loaded, especially the DOM tree?
I placed the script at the very bottom of the page, right before </body>, so that can't be it. And as I said, it works in XHTML 1.0 Strict.
in fact, i would like to ask why XHTML 1.1 ban target?
Because with frames banned, it became practically useless, excluding the _blank value. And _blank itself was removed for device independance's sake. I mean, you can't open a new window in a mobile browser. They usually just don't support them, speaking of which, this same JS above may not work for such devices. aspnetguy's variant has a better chance there.I once posted a script I found on a blog which was just perfect. It used window.open() instead of creating a target and again searched for links with rel="external". I'm not sure, but I think it even detected availability of window.open for device independance's sake. Now if I could only just find it again...
Link to comment
Share on other sites

I placed the script at the very bottom of the page, right before </body>, so that can't be it. And as I said, it works in XHTML 1.0 Strict.Because with frames banned, it became practically useless, excluding the _blank value. And _blank itself was removed for device independance's sake. I mean, you can't open a new window in a mobile browser. They usually just don't support them, speaking of which, this same JS above may not work for such devices. aspnetguy's variant has a better chance there.I once posted a script I found on a blog which was just perfect. It used window.open() instead of creating a target and again searched for links with rel="external". I'm not sure, but I think it even detected availability of window.open for device independance's sake. Now if I could only just find it again...
try <body onload="func();">i think searching for links with rel="external" in DOM tree is a bit over to achieve just a pop up/open in a new browser. Is there any benefit that make us to write so many codes instead of just window.open()?
Link to comment
Share on other sites

try <body onload="func();">i think searching for links with rel="external" in DOM tree is a bit over to achieve just a pop up/open in a new browser. Is there any benefit that make us to write so many codes instead of just window.open()?
Those events are to be deprecated too, in favor of XMLEvents, and also because such events are not essentially part of the structure of a document, the same way presentational attributes (think "bgcolor") are not.
Link to comment
Share on other sites

Those events are to be deprecated too, in favor of XMLEvents, and also because such events are not essentially part of the structure of a document, the same way presentational attributes (think "bgcolor") are not.
excuse me, u mean rel or window.open() is deprecated?i think, using javascript for target="_blank" is quite handy. Although just target="_blank" is much shorter than onclick="return !window.open(this.href)", it is still acceptable. But here comes the headache is, it works only if the client's browser get JS enabled. But i bet there is no way to open things in a new window without target="_blank" _and_ JavaScript. Of course, if anyone can suggest one, it will be great!!
Link to comment
Share on other sites

excuse me, u mean rel or window.open() is deprecated?
Neither. I meant the on* attributes - onclick, onmouseover, etc.They are to be deprecated from XHTML, though they are (and will of course remain) valid in XHTML 1.0 and XHTML 1.1. I mean that they will be deprecated in future versions of XHTML.
i think, using javascript for target="_blank" is quite handy. Although just target="_blank" is much shorter than onclick="return !window.open(this.href)", it is still acceptable. But here comes the headache is, it works only if the client's browser get JS enabled. But i bet there is no way to open things in a new window without target="_blank" _and_ JavaScript. Of course, if anyone can suggest one, it will be great!!
If a client doesn't have JS enabled, they'll still open the page in the same window. That's still better then not opening the link at all.There's a draft for a new CSS property that will replace the target="_blank" functionality and impove it too. It will allow you to explicitly state whether you want the window opened in a new window or a new tab. Right now, with JS or target="_blank", this is browser dependant. Of course, this new property is still not supported in any browser, so yes. Right now, there isn't a way without target="_blank" or JS.I'm talking about the CSS3 hyperlinks module.
Link to comment
Share on other sites

cool!! longed for CSS3!!! is it that i still cannot use it now? :)* but i bet, although CSS3 is damn cool, but not support in old browsers (where most people are using IE6/7 these days) will also no help :):(:)why on* are going to be deprecated? so, how can we capture mouse/keyboard events??

Link to comment
Share on other sites

cool!! longed for CSS3!!! is it that i still cannot use it now? :)* but i bet, although CSS3 is damn cool, but not support in old browsers (where most people are using IE6/7 these days) will also no help :):(:)
Yes. Legacy browsers. This will remain as a problem even when CSS3 is eventually implemented across browsers. I guess with time, they will get less and less important the same way we never consider IE4 when making layouts.
why on* are going to be deprecated? so, how can we capture mouse/keyboard events??
I told you.
in favor of XMLEvents, and also because such events are not essentially part of the structure of a document, the same way presentational attributes (think "bgcolor") are not.
Even if XMLEvents were not supported, you still have the JS objects associated with those events, the same way for example the line
window.onload = externallinks;

applies to the window on the onload event.

Link to comment
Share on other sites

You guys are still not getting the fact that forcing another page to open in a Browser violates the intent of removing the target=_blank.The Client should be the one to decide whether they want an additional window/tab opened. Whether these javascript codes work properly, or not, the Standards say that you should not be opening tabs or windows in the Browser, hence the deprecation of the option to do so...* end rant here *Hi! Having fun yet?

Link to comment
Share on other sites

You guys are still not getting the fact that forcing another page to open in a Browser violates the intent of removing the target=_blank.The Client should be the one to decide whether they want an additional window/tab opened. Whether these javascript codes work properly, or not, the Standards say that you should not be opening tabs or windows in the Browser, hence the deprecation of the option to do so...* end rant here *Hi! Having fun yet?
The WCAG say to avoid pop-up windows without informing the user and until user agents allow users to turn off spawning of new windows. Using JS to pop-up windows may fail at times exactly because of such settings made by the user, in which case the link will open normally. At least all browsers allow users to turn off JS if they want. Soon they'll probably make setting to disallow scripts to create new windows. And besides, if the user is informed the link should open in a new window - what the heck? You're still following the guidelines, right?I see no benefit in creating external links, such as the validation pages, that open in the same window, from which the user will feel even more lost, then if the link opened in a new window. Regardless of whether the user has dissability problems. For such links, it may not even be needed to inform the user, especially if the link is an image (as the case with the validation buttons).
Link to comment
Share on other sites

  • 3 weeks later...
You guys are still not getting the fact that forcing another page to open in a Browser violates the intent of removing the target=_blank.The Client should be the one to decide whether they want an additional window/tab opened. Whether these javascript codes work properly, or not, the Standards say that you should not be opening tabs or windows in the Browser, hence the deprecation of the option to do so...* end rant here *Hi! Having fun yet?
Yes, you have your point. But i see the other way round.For some sites, those links are supposed to open in another browser, or pop up, but they open in the same browser that makes me inconvenient. For example, the Google search. Most likely not every result matches exactly what i want. Or my research needs to combine several website results. So i need to "Ctrl+click" to open them in new browsers/tags. Otherwise, i will keep on going to-and-fro which makes me dizzy. It is still fine with Google because i am used to it and i know i have to keep my finger on the Ctrl button. What if i encounter a new site that i did not visit before? Most likely, going back by the browser back button may NOT really appear exactly the page you saw. There may be some dynamic contents, or random elements, that changed.And also, for some "tips" or "helps" or "user guidelines", i really cannot see how opening in the same browser is better than giving a small pop up.Some may argue that we can still have pop up by JS. But what if some clients are too cautious about scripts and disabled JS? We need not to care them and just put a line in our page that "no scripts, no tips"?
Link to comment
Share on other sites

For some sites, those links are supposed to open in another browser, or pop up, but they open in the same browser that makes me inconvenient. For example, the Google search. Most likely not every result matches exactly what i want. Or my research needs to combine several website results. [...]
What if i encounter a new site that i did not visit before? Most likely, going back by the browser back button may NOT really appear exactly the page you saw. There may be some dynamic contents, or random elements, that changed. [...]
And also, for some "tips" or "helps" or "user guidelines", i really cannot see how opening in the same browser is better than giving a small pop up. [...]
As a user I personally have these same preferences--but I can make a choice about opening in new window/tab or not. And as a matter of fact I almost always open everything in a new tab. The ability of a site to open tabs/windows/popups itself does not add anything to my browsing experience, and can be a nuisance, so I personally would prefer it never to happen. I welcome the initiatives to reduce/eradicate it.
[...] i know i have to keep my finger on the Ctrl button. [...]
You do know you can just click the scroll wheel button instead of ctrl-click?
Link to comment
Share on other sites

As a user I personally have these same preferences--but I can make a choice about opening in new window/tab or not.
What about users that are not as savvy as we are? Seeing such people everyday at work, I can guarantee you most can't make that choise i.e. even if they know how to open a new browser window/tab from a link to begin with, they are always uncomfortable doing it and prefer to rely on the site for it.
And as a matter of fact I almost always open everything in a new tab. The ability of a site to open tabs/windows/popups itself does not add anything to my browsing experience
To yours and mine, perhaps. Again, think of others. It adds to their experience, and it's not likely to do anything bad to yours, so where's the problem?
You do know you can just click the scroll wheel button instead of ctrl-click?
I don't know about midnite, but I sure didn't know that. The point of lesser education in action :) . If I didn't know that, how likely you think it is for someone that's not with computers to know that, or the CTRL+Click way?
What if i encounter a new site that i did not visit before? Most likely, going back by the browser back button may NOT really appear exactly the page you saw. There may be some dynamic contents, or random elements, that changed.
Tell me about it. This is especially annoying for sites that use some sort of easily expiring session. They lead you to a page you're not sure will be what you need, and if you go back you see "Session Expired" error message. Now that's just VERY annoying.
Link to comment
Share on other sites

[...] To yours and mine, perhaps. Again, think of others. It adds to their experience, and it's not likely to do anything bad to yours, so where's the problem?
Actually it's others I'm mainly thinking of! (More on this below.) Though let's not forget scripts can (and do) open a popup/window at times other than when I click a link, which may navigate a site I know nothing about and attempt malicious/intrusive actions. But that aside, you are absolutely right, I get by--but it's others I am thinking of, and to answer your question, the problem is: I don't think it does add to their experience, it detracts, if they can't rely on sites to behave consistently when a link is clicked. (And of course sites will always be coded in a wide variety of ways, because the web is the web, and so the only place to enforce this consistency is in the browser/xhtml/scripting technology itself.)
[...] even if they know how to open a new browser window/tab from a link to begin with, they are always uncomfortable doing it and prefer to rely on the site for it.
But if they develop the habit of relying on sites for this, they are in for a shock! Because, as you rightly say:
[...] They lead you to a page you're not sure will be what you need, and if you go back you see "Session Expired" error message. Now that's just VERY annoying.
I'd say as a general principle users are always more comfortable with what they already know, but if they find enough incentive to make an effort (and it is hard) to become savvy with new ways of doing things, the effort is rewarded--and ultimately they are even more comfortable with what they can do (and more likely to make the effort next time--a virtuous circle). I'm not talking about technically savvy (remember that every computer user went through this process to, say, use a mouse, in the first place) just savvy about the effect of navigation choices on that user's browsing experience. Increasingly sites are coded such as to make the back button redundant, with broken sessions resulting from using it. This is not going to go away and is now the norm for many things like online banking, PayPal etc. The frustration this causes is making users savvy about the effects of their navigation choices, including choosing to open a new window to avoid losing the current one. Tabbed browsing is a big step forward in putting this back in the hands of the user, since tabs open quickly and allow far better screen organization than multiple windows (something else novice users are uncomfortable with). And since users can't rely on all sites to open the right windows for them, I think the fact that some sites do has now become not a help, but a hindrance, in this overall process of enabling/incentivising users to become savvy about making navigation choices themselves.
You do know you can just click the scroll wheel button instead of ctrl-click?
I don't know about midnite, but I sure didn't know that. The point of lesser education in action :) .
Enjoy! :)
Link to comment
Share on other sites

It is exactly those payment sites that cause the biggest frustrations in users of all ages, especially seniors that just can't learn. The older a person is, the harder it's for them to learn - a science fact even. And then again, even I often fall into this expired session trap, so I'm no better. Users that are not willing to learn (as most are) won't learn... they'll just try again from the start of the session, annoyed and all, this time making another click depending on their next best guess... if that fails too, repeat.Even this non-learning behavior works for sites like PayPal that don't have decent alternatives. It also works for the sites at which I fall into that trap - I often search for drivers on certain devices, and go to the manufacturer's site at which if I go back because I chose a wrong model, says session expired. It won't work for sites that DO have alternatives though. In such cases, users will just leave annoyed, or keep going only until they find another alternative, after which you're done.If you open a new window from a window that easily expires, you can ensure the user may just close it (annoyed at first that there's no back button) after which he can select a new link from where they were. Yes, they could do that themselves, but many are just either in a hurry so they won't know or care if you have an easily expiring session or not (i.e. they're ignorant; I and everyone I know sure are), or they just don't know they can open a new window/tab.

And since users can't rely on all sites to open the right windows for them, I think the fact that some sites do has now become not a help, but a hindrance, in this overall process of enabling/incentivising users to become savvy about making navigation choices themselves.
If you open everything in a new tab or window, this would really become annoying. But opening key pages in new windows like external sites and easily expiring sessions is still, and would remain, helpful. Anything that's not in the eyesight of the user is considered "advanced" for newbies, and "time consuming" for advanced users. If I'm in a hurry, I only click thru, Google being an exception to that rule since I'm already close with it. All other sites however should be making the choice for me, as I go to them for nothing more then the information they have, whereas I go to Google for any information any other site can have.
Tabbed browsing is a big step forward in putting this back in the hands of the user, since tabs open quickly and allow far better screen organization than multiple windows (something else novice users are uncomfortable with).
That doesn't yet seem to be the case for IE(7) users i.e. REAL newbies. Most I know haven't seen tabs before, so are yet uncomfortable with them. Imagine just getting used to something (opening new windows), and right then be hit with something new ("That doesn't open in a new window? Where did it go? Back there (in a tab) you say? What's that?"). Also, tabs seem to open a bit slower in IE then new windows. Perhaps legacy is the reason. And honestly said, I also feel more comfortable using new windows, rather then new tabs.
I'd say as a general principle users are always more comfortable with what they already know, but if they find enough incentive to make an effort (and it is hard) to become savvy with new ways of doing things, the effort is rewarded--and ultimately they are even more comfortable with what they can do (and more likely to make the effort next time--a virtuous circle).
That's true, but some just don't want the reward and simply WON'T make incentive.
Link to comment
Share on other sites

I'd say as a general principle users are always more comfortable with what they already know, but if they find enough incentive to make an effort (and it is hard) to become savvy with new ways of doing things, the effort is rewarded--and ultimately they are even more comfortable with what they can do (and more likely to make the effort next time--a virtuous circle).
That's true, but some just don't want the reward and simply WON'T make incentive.
That's also true (and ultimately of course it's their decision), but we make things worse for them, not better, if we perpetuate the illusion that they can trust sites to make window-opening choices for them.
Link to comment
Share on other sites

<a href="http://www.google.com" onclick="return !window.open(this.href,'_blank','width=400')">Google It!</a>

this would be cool enough.i do understand that opening a new window is annoying and can make people lost sometime. But what i was (originally) talking about is1. for pop up tips2. for external linksi still think that pop up tips is best to be opened in new windows. They should be smaller than a normal window, such that users can have a clear picture that it is a pop up, and the main content is just beneath. it will be even better if i add a "Close Tips" button in it.For external links, i may compromise. Because a site is opening, no matter in terms of weight, or the size of the window, would be similar to the previous one, opening it in new windows (without "Back button" compatibility) can be a problem for n00b. For this, we can learn from Wikipedia that adding a "External" logo, or else. :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...