Is 'target' valid XHTML?
_shane_ - December 9, 2008 - 22:08
| Project: | External Links |
| Version: | 6.x-1.6 |
| Component: | Code |
| Category: | feature request |
| Priority: | minor |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
What about having jQuery use window.open on the 'ext' class? Since 'target=_blank' is not valid XHTML Strict.

#1
No it's not valid XHTML strict, but because this attribute is added after page load your pages will still pass validators. Though you're right, since we have a convenient class, we could easily switch to using it instead.
#2
As mentioned in #386988: External Links: add open in new window to statusbar, a good argument not to use JavaScript is the nice status bar message provided by Safari:
Which we only get if using target="_blank" since Safari won't actually interpret a JavaScript popup script that would do the same thing. Of course Safari is the only browser that currently does this, so it's not that strong an argument.
#3
I also want XHTML strict compliance.
Would it be possible to introduce an optional settings field where one can introduce one's favourite Javascript "open in new window" hack (rather than hacking it into the external links module, as I'm about to do) ?
#4
Or should I say hack external.js.
if (Drupal.settings.extlink.extTarget) {// Apply the target attribute to all links.
$(external_links).attr('target', Drupal.settings.extlink.extTarget);
}
If the work is done by JavaScript anyway, which not simply have one of the usual JavaScript tricks ?
#5
My hack fails:
if (Drupal.settings.extlink.extTarget) {
//ORIG $(external_links).attr('target', Drupal.settings.extlink.extTarget);
//override to force XHTML compliance FAILS
$(external_links).attr('onclick', 'window.open(this.href); return false;');
}
It seems that anything written to javascript attributes (like 'onclick') is filtered out.
Does anybody know why, or how I can ensure that 'target' is not used, rather the javascript instead ?
#6
This seemed to work for me (with all caveats, I'm not a jQuery expert):
$(external_links).bind("onclick", function() { window.open(this.href); return false;});#7
Popup blockers?
#8
If you can't get the JS to work, assign the "rel" attribute to the link instead of "target", and then use one of the many JS "open in new window" scripts out there, that act on the rel attribute.
This, of course, once again raises the specter of the issue of W3C having deprecated the target attribute. It kind of makes sense to make that a user driven decision, but it's about 4 years ahead of both the general competency of web users at large--to be savvy enough to open external links in a new tab or window--and the expectations of 90% of web site buyers. It's a real pain in my hide, let me tell you.
#9
As a follow up to that, it is possible to obtain XHTML compliant markup without altering the extlinks module at all. Disable the extlinks "open external links in a new window" in the module's admin. You will still have a class="ext" for external links. Then, use whatever method you want to include some Javascript on your page; I have a main.js file that I have a few functions stored in that I am including with drupal_add_js() in my template.php file. In that, I am calling an externalLinks() function in the jQuery equivalent of page.onload:
$(document).ready(function() {
externalLinks();
//other stuff here
});
And the function (adapted from http://perishablepress.com/press/2007/11/20/open-external-links-as-blank...) is:
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("class") == "ext" ))
anchor.target = "_blank";
}
}