PDA

View Full Version : How to replace target="_blank" for XHTML strict w3 compliance


webado
11-21-2006, 02:55 PM
There are those proponents of opening links (especially external ones) in a new window and then the a purists who think it's an abominaiton ;)

For those who prefer the ability to open links in a new window yet keep a valid XHTML strict doctype, I stumbled onto an elegant piece of code that will allow just that.

Instead of using target="_blank" on a link, use rel="external" and then have javascript that performs a little miracle.

Plainly put if the visitor's browser is javscript enabled the link with rel="external" will behave like target="_blank", otherwise it's inert.

See it here: http://www.sitepoint.com/article/standards-compliant-world/3



The only other little problem to solve is to be able to specify both rel="external" and rel="nofollow" - maybe like rel="external nofollow" together.
I haven't looked at what if any javascript changes need to be done to allow this.

I am currently using this method for all external links on www.webado.net which I have converted to xhtml strict lately and it works like a charm.

JWJ
11-21-2006, 03:09 PM
I know I'm stepping out of my league here but I feel I must comment on this. Surely, coding to the highest level possible is a 'purist' activity and therefore using target=_blank is a bit of a contradiction. Rather than code to 'strict' levels and then concoct a work-around for something you're not supposed to do, wouldn't it be better to code to 'transitional' level and use target_blank in the conventional manner?

webado
11-21-2006, 03:52 PM
Well yes and no.

The "purist" idea is in my opinion a possibly wrong interpretation of the purpose this standard is meant to have.

I believe it's essentially in order to help robtos crawl better since sending them off here and there with target="_blank" may be somewheat counterproductive.

Other purists may have taken it to mean let user decide if a link is a new window or not - well ok, but I and possibly countless others don't go around right clicking links so that we get to pick new or same. It bugs me no end when I go to an external link and by pure force of habit I close the window after seeing it's not on the same site - and then I've ended up closing the whole thing.

Anyway, it's to take or to leave.

I will always open external links in new windows - and that's that.

Now that I went through the exercise of converting to xhtml strict (which may have benefits other than just academic, but yet to be discovered by yours truly) I don't intend to change that habit ;)

So this is a valuable workaround for me and for anybody who wants the same thing.

It can also be used in all other doctypes to accomplish the same thing. Robots will have a nice smooth crawl with no new windows and users will have the navigation I intend for them to have - whether they like it or not ;) It's part of my designs, so if they don't like it, tough :lol:

Oh and I did manage to modify the script to allow rel="nofollow external" and "rel="external nofollow" as well.

So my script called external.js is now:


function externalLinks() {

if (!document.getElementsByTagName) return;

var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
var relvalue = anchor.getAttribute("rel");

if (anchor.getAttribute("href")) {
var external = /external/;
var relvalue = anchor.getAttribute("rel");
if (external.test(relvalue)) { anchor.target = "_blank"; }
}
}
}
window.onload = externalLinks;

JWJ
11-21-2006, 04:19 PM
Thanks for sharing your thoughts on that.

When I was converting some pages to strict I came across this very problem. In one case I decided to go strict and dispense with the new window, in another case I decided I wanted the new window so I left the page as transitional. As far as I'm concerned 'strict' is 'strict' and 'strict' with a bit of a workaround is less than 'strict'.

Of course, my approach is also a work-around .... it works around the fact that I don't know javascript. ;)

webado
11-21-2006, 06:48 PM
Of course, my approach is also a work-around .... it works around the fact that I don't know javascript. ;)

Neither do I but I can bluff may way around a bit ;)

Just you wait, I'm working on making colored scrollbars that even the w3c will accept - or rather ignore blissfully ;)

coombes
11-22-2006, 10:58 AM
Purism for me is a site that works, looks right on very browser and does what's expected of it. If I get what I want but it isn't 100% valid... the site still works.

-=Seth=-
11-22-2006, 01:09 PM
coloured scrollbars that even the w3c will accept
that would definately be cool

webado
11-22-2006, 02:42 PM
Purism for me is a site that works, looks right on very browser and does what's expected of it. If I get what I want but it isn't 100% valid... the site still works.

Heh, it works in all browsers (at least non-mac stuff), you get what I want you to get out of it ;) and it is 100% valid too - how's that for purity? :lol:

SLORider
02-03-2007, 04:32 PM
I really don't see the point in serving "supposedly valid" XHTML, then making it invalid via JavaScript. In fact, I avoid script like the plague as you never know if it's enabled on the client or not.

If you modify the DOM after the page is served, your page is still not valid XHTML. If browsers ever start enforcing the XML DTDs you'll see this. I think it's just fooling yourself by thinking passing the W3C Validator is the gold standard.

I don't see the point of robots having a "smooth crawl". They probably don't even look at target="_blank". If they do, it's a good hint the link is external.

On the other hand, I like target="_blank" and use it for external links. I'm trying to supply an external link icon http://tek4.com/ico/ext-link.png which I think is the best etiquette if you're going to use target="_blank".

jukcoder
02-03-2007, 08:18 PM
I was to use this trick till I decided to remove all popups in my web site. If I am refererring to an external URL I have to add a pop-icon to the link : humans and search engines won't have the right to complain.

webado
02-03-2007, 10:17 PM
It's not a pop-up. It's opening the window using target="_blank" which is not the same as window.open which is javscript (that's what a pop-up is).

jukcoder
02-03-2007, 11:42 PM
You are right. They are not pop-ups, but pop-up killers in my browser mute all external windows with target="xxx"!
Have a look at yahoo portal and you will ,always , see just one single page.

webado
02-04-2007, 04:53 AM
No pop-up killer should kill a window opened as target="_blank". What kind of rubbish pop-up killer do you have?

Maybe you have a browser option to always open in the same tab or window regardless.

sabaka33
02-25-2008, 11:30 PM
I know this thread is a bit old... but.. how would one accomplish something like:

<noscript>
<a href="link some guide to enable javascript" target="_blank">Please enable Javascript in your browser. Click here for help.</a>
</noscript>

Consider the following:

Using DOCTYPE with deprecated target attribute
Javascript is disabled
You want to open a pop-up window explaining how to enable javascript (http://www.google.com/support/bin/answer.py?answer=23852)


or maybe there's another way of accomplishing this?

webado
02-26-2008, 01:09 AM
Sorry I don't understand why you need to bother with this.

If the guy doesn't have javsacript enabled anything depending on javascript won't work obviously. You should never ever demand that a user have javascript enabled just to browse your site. You can advise maybe that for a best experience js is recommended.

So they cannot open pop-up winodws. OK, either no window is opened (e.g. if it was an ad meant to be in js only, the way it should be, it won't open) or it's opened normally not as pop-up (the proper way, that is for regular links that you actually want to open).

As for forcing the target="_blank" using js for the pure benefit of still having a valid xhtml strict document, this is candy. It's not a pop-up window anyway. it's substitution code.


Your links will use somehting like rel="external" which has no meaning for html, and js then converts that to target="_blank".

If js is off this substitution doesn't happen. The link will be opened in the same window, not a new one. This is the way many people prefer links to open in any case, adn the w3c advises this is the proper way and to let users decide if they want or not a new window or tab - which they'd do with the right button of the mouse.


As for your code use it as is for whatever purpose you have it unless you are using an xhtml strict doctype in which case target="_blank" will not be valid.

But if your whole reason was to force people to open links in new windows this is a lame excuse. Similarly if you needed them to accept pop-ups this is the best example of why people would want to disable js.

sabaka33
02-26-2008, 05:15 PM
Let me clarify :smile:
I plan to add a slide show created with javascript (mootools (http://demos.mootools.net/Asset.images)). This will require js to be enabled. I thought I would just pop-up a guide on how to enable js so that the user would not have to navigate back to the page after js is enabled, just close the pop-up. I guess I'll just print the entire "how to" on the slide show page.

webado
02-26-2008, 09:47 PM
Those slideshows usually also work with js off. Except they aren't slide shows. But the basic function is there: see an image at a time.

In any case robots are those most likely to not have js on. A slideshow is not robot material.

donecweb
02-26-2008, 11:28 PM
I thought I would just pop-up a guide on how to enable js so that the user would not have to navigate back to the page after js is enabled, just close the pop-up.
Why use javascript for the popup? Why not use a small CSS box popup like example....
<a class="moredata" href="#moredata" style="color : blue; font-weight : normal;"><u>produces better quality</u><span>Once I had the text about the right size I needed to add some texture. </span></a>The CSS for that is.....moredata {
position: relative;
z-index: 0;
text-decoration : none;
font-weight : normal;
}


.moredata:hover {
background-color: transparent;
z-index: 50;
text-decoration : none;
}


.moredata span {
background-color : yellow;
border-bottom-color : black;
border-bottom-style : solid;
border-bottom-width : 1px;
border-left-color : black;
border-left-style : solid;
border-left-width : 1px;
border-right-color : black;
border-right-style : solid;
border-right-width : 1px;
border-top-color : black;
border-top-style : solid;
border-top-width : 1px;
color : #000;
left : 0px;
padding-bottom : 5px;
padding-left : 5px;
padding-right : 5px;
padding-top : 5px;
position : absolute;
text-decoration : none;
top : 0px;
visibility : hidden;
width : 250px;
}


.moredata:hover span {
visibility : visible;
}
This shows a link on the page that says "produces better quality" and when your cursor is on top of the link a box popup appears that says "Once I had the text about the right size I needed to add some texture. " with a colored background.

Or a small CSS box with overflow set to auto which will give you the appearance of a small frame within your page that has it's own scroll bars to allow visitors to scroll through the guide without disrupting your page layout much.

sabaka33
02-27-2008, 06:01 PM
Why use javascript for the popup? Why not use a small CSS box popup

I haven't thought of it :oops:

Just tried that idea, no luck. Unless I'm missing something, the only way to make this work requires a click pseudo class which, to my knowledge, does not exist. Using hover to display the guide works until you try to follow it. As soon as you navigate your mouse to the menu, you take your pointer off the guide and it disappears. I tried using keyboard (Alt + t) on both IE7 and FF2 and the guide was visible until the options box opened (it worked on Opera 9.26 though). If you want to see what I mean, try the following (complete html page):
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
.moredata {
width: 80%;
text-decoration: none;
font-weight: normal;
cursor: pointer;
}

.moredata div {
background-color : yellow;
border: black solid 1px;
color : #000;
padding: 5px;
display: none;
text-decoration : none;
height: 500px;
overflow: auto;
cursor: auto;
}

.moredata:hover div {
display: block;
}
</style>

<title>this is only a test</title>
</head>

<body>
<div class="moredata">
<p>Help?</p>
<div>
<h2> How do I enable JavaScript?</h2>
<p>The following instructions describe how to enable JavaScript in your browser. If your browser isn't listed, please consult its online help pages.</p>
<p>Internet Explorer (6.0) </p>
<ol>
<li>Select <strong>Tools</strong> from the top menu.</li>
<li>Choose <strong>Internet Options</strong>.</li>
<li>Click <strong>Security</strong>.</li>
<li>Click <strong>Custom Level</strong>.</li>
<li>Scroll down till you see section labeled <strong>Scripting</strong>.</li>
<li>Under <strong>Active Scripting</strong>, select <strong>Enable</strong> and click <strong>OK</strong>.</li>
</ol>
<p>Internet Explorer (7.0)</p>
<ol>
<li>Select <strong>Tools</strong> &gt; <strong>Internet Options</strong>. </li>
<li>Click on the <strong>Security</strong> tab. </li>
<li>Click the <strong>Custom Level </strong>button. </li>
<li>Scroll down to the <strong>Scripting</strong> section.</li>
<li>Select <strong>Enable for Active Scripting and Scripting of Java Applets</strong>. </li>
<li>Click <strong>OK</strong>. </li>
<li>Select <strong>YES</strong> if a box appears to confirm. </li>
<li>Click <strong>OK</strong>. Close window. </li>
<li>Reload page.</li>
</ol>
<p>Mozilla Firefox (1.0) </p>
<ol>
<li>Select <strong>Tools</strong> from the top menu.</li>
<li>Choose <strong>Options</strong>.</li>
<li>Choose <strong>Web Features</strong> from the left navigation bar.</li>
<li>Select the checkbox next to <strong>Enable JavaScript</strong> and click <strong>OK</strong>.</li>
</ol>
<p>Mozilla Firefox (2.x)</p>
<ol>
<li>Open Firefox. </li>
<li>On the <strong>Tools</strong> menu, click <strong>Options</strong>. </li>
<li>Click <strong>Content</strong> in the <strong>Options</strong> list. </li>
<li>Under the <strong>Content</strong> section, check the box next to <strong>Enable JavaScript</strong>. </li>
<li>Click the <strong>Advanced</strong> button to open the <strong>Advanced JavaScript Options</strong> box. </li>
<li>Check the appropriate boxes under <strong>Allow scripts to</strong>. </li>
<li>Click <strong>OK</strong>. </li>
<li>Click <strong>OK</strong>.</li>
</ol>
<p>Apple Safari (1.0) </p>
<ol>
<li>Select <strong>Safari</strong> from the top menu.</li>
<li>Choose <strong>Preferences</strong>.</li>
<li>Choose <strong>Security</strong>.</li>
<li>Select the checkbox next to <strong>Enable JavaScript</strong>.</li>
</ol>
<p>Please keep in mind that upgrading your browser or installing new security software or security patches may affect your JavaScript settings.</p>
<p>source: http://www.google.com/support/bin/answer.py?answer=23852</p>
</div>
</div>
</body>
</html>

The whole reason I brought up that pop-up thing is to have the "guide" in a centralized location (PHP can fix this :)) to be linked to from multiple pages and updated easily while not navigating the user away from the page they are looking at.

Thanks for all the comments/suggestions, I think I'll leave this alone now. Oh, and I didn't mean to hijack the thread :sad:

webado
02-27-2008, 06:54 PM
Pure css pop-up:
http://meyerweb.com/eric/css/edge/popups/demo.html

From the building pillar of css I believe.


Oh and xhtml 1.1 doesn't work in IE.

webado
02-27-2008, 07:11 PM
To suggest that Help? is clickable add this to css:

#help {cursor: pointer;text-decoration:underline; color: blue; background-color: inherit;}


And change Help? to

<a id="help">Help?</a>

donecweb
02-27-2008, 10:39 PM
OK sabaka33, then why not place a small CSS box that your visitors can scroll through if they want to where you need it. You can see an example at.....
http://bestwebstop.webcntr.info/
The box on the right could be any size you want it to be. This method has the advantage of not being seen as hidden text.

Sarve
02-28-2008, 11:07 PM
I don't see the point of robots having a "smooth crawl". They probably don't even look at target="_blank". If they do, it's a good hint the link is external.


I have a few pages on my site that are linked to, by other pages on the site, with the target="blank" and the bots seem to be finding them. One of them got indexed, which wasn't a good idea. The only thing on that page are the words "Coming Soon!" in big blue letters. I use the page to notify visitors that the particular topic they are wanting to look at will be on the site soon.