I received a question this morning from someone asking:
In XHTML 1.1 we cannot use attribute 'target=blank', so what is the solution?
The solution is to use regular links, but to make them open into a new window using JavaScript. To do this, we can add something to the links to flag them as being special, perhaps a class called new-window:
<a href="page.html" class="new-window">Page</a>
Then, use JavaScript to find all the links that have this class name and tell them to open in a new window:
window.onload = function() {
var links = document.getElementsByTagName('a');
for (var i=0;i < links.length;i++) {
if (links[i].className == 'new-window') {
links[i].onclick = function() {
window.open(this.href);
return false;
};
}
}
};
or using jQuery:
$(function(){
$('a.new-window').click(function(){
window.open(this.href);
return false;
});
});
If you have any other questions like this, feel free to ask me and I'll be happy to answer them here.
If you really do want to open new windows, shouldn't you consider using another doctype, for instance a transitional one, which does not require you to not open external links with the target attribute?
Nice code anyway. :)
Sure, you could use XHTML 1.0 Transitional. But if you want to use XHTML 1.1 and have links open in a new window, you're stuck using JavaScript to do this.
This is exactly the same solution which was posted in a sitepoint article titled New-Window Links in a Standards-Compliant World. Although, instead of using a class name, it uses the more semantic rel attribute.
The most correct answer is "don't open a new window". Opening new windows is bad for usability because it breaks the normal behaviour of web pages (for example, back buttons). If the user wants a link opened in a new window, the user knows how to do it themselves.
The second most correct answer is "don't use XHTML 1.1". Any benefits received from using XHTML are offset by the fact that the page won't work for 80% of users out there.
The third most correct answer is "use the target attribute anyway". I don't think any browsers out there will ignore the target attribute just because you claim to use XHTML 1.1 in the DOCTYPE. If you're already sending XHTML as text/html for IE users, what's wrong with another non-standard "compromise"?
I would say that using the script provided is fine as a fourth-best solution, but if you MUST open a new window, and you're standards-crazed enough that your site MUST be valid XHTML 1.1, why aren't you standards-crazed enough to reject the usage of non-standard ECMA script like window.onload, element.onclick, and window.open?
"why aren't you standards-crazed enough to reject the usage of non-standard ECMA script like window.onload, element.onclick, and window.open?"
I try to write code that people can easily copy and paste, and I want to keep the examples relatively simple. I always recommend people use addEvent instead of onclick, and addDOMLoadEvent instead of window.onload. Those who already know this will understand to use these, and those who don't won't be hurt by using old school JavaScript (until they wonder why their other window.onload function stopped working, I suppose).
That's why I give the jQuery example as well, so there's at least one "proper" example ;)
You chopped up my question. I wasn't asking "why are you using non-standard ECMA Script?". I was asking "Why are you using non-standard ECMA Script to avoid using non-standard HTML?" Even your jQuery example is non-standard with window.open, not to mention whatever jQuery itself does.
Reworded: given the choice between simply defining the non-standard target attribute in the HTML or writing a more complex, non-standard script which also has all the disadvantages scripting brings to the table, why would you choose the script?
I didn't mean to take your quote out of context, I just wanted to address why I use 'window.onload' in examples even though I'd never use it myself.
I agree with you that it's better to rely on a non-scripting solution, and that XHTML 1.1 isn't appropriate to use yet.. but maybe one day this blog post will be relevant.
"but maybe one day this blog post will be relevant."
Maybe, but maybe the target property of CSS 3 will gain support before XHTML 1.1 does.
http://www.w3.org/TR/css3-hyperlinks/#target0
Here goes, this is my first ever source code question so bare with me. I'm trying to embed my delicious links in a Joomla! wrapper. Delicious gave me this...
<script type="text/javascript" src="http://del.icio.us/feeds/js/jexzer?title=my%20del.icio.us;icon;name;showadd"></script>
<noscript><a href="http://del.icio.us/jexzer">my del.icio.us</a></noscript>
It works great but the links open in the same window which don't fit inside my Joomla! site. I want them to open in new windows. I tried your code in every way I could think of and couldn't get it to work. I very new at this and would appreciate some help. Thanks.
@Jonathan - the class name for delicious links is 'delicious-link', so you'd need to use that instead of 'new-window'.
If you already tried that, perhaps the problem is the script is being loaded too soon. Try adding it in a timeout to give some time for the delicious script to run, like so:
window.onload = function(){
setTimeout(function(){
// code above that changes the delicious links
}, 5000);
};
You could also try the approach of using setInterval to check the page every second until the links you want show up, then attaching the events and clearing the Interval.
You Rock. How did you know that 'delicious-link' was the class name?
Many thanks,
-JE
This code was created by someone else...not trying to bust your balls here or anything. At least give credit where credit is due:
http://www.456bereastreet.com/archive/200610/opening_new_windows_with_javascript_version_12/
@Terry - Obviously I didn't copy Roger's script, just wrote my own code to accomplish the same thing. Actually, I've never seen Roger's solution until now. But his is also nice (albeit much more complex).
You are correct...and like I said, I wasn't trying to bust your balls on this. If you look at other versions and other people's customizations...they all derived from his code (just like all versions he published pre 1.2 or which ever version it's on.)
When I saw your code...and logic...the first thing that came to mind was his code. You may very well have written it all on your own, but it's similar enough that I wasn't the only one that noticed.
Side note: I do enjoy your site and use it when I find useful information. Keep it up!
You inserted some text in "blockquote" TAG so this page is not valid XHTML:
Error Line 36 column 12: character data is not allowed here.
<blockquote>In XHTML 1.1 we cannot use attribute...
..."solution, and that XHTML 1.1 isn't appropriate to use yet.."
Can you guys please elaborate??
Thanks! :)
There are yet more ways to use the target attribute in XHTML 1.1, without script and totally standard conforming. After all, XHTML is extensible! http://learningtheworld.eu/2007/xhtml-with-target/
it wont be more simple:
<a href="http://www.blablabla.br"
onclick="this.target='_blank'">
Yes it will:
<a href="http://www.blablabla.br"
onclick="target='_blank';">
If the user viewing the page has javascript disabled, then the hole thing doesn't work.
So, I think, javascript is not the solution to the <target="blank"> validation problem with XHTML strict 1.1
In other words, let's hope that W3C gives a solution in the future...
Comment 19 works perfect.
onclick="this.target='_blank'"
This works with xhtml 1.1 and is valid for http://www.w3.org.
Thank You
Just another reason why I stick to nothing newer than HTML 4 with JS and CSS as needed.
After reading this post the code is slick for jquery.
Here is a mod:
//get the click to open popup
$(document).ready(function() {
$('a[rel*=link]').click(function(){ window.open(this.href);
return false;
});
});
Anyway this will open up a new window based on the 'rel' attribute. Web semantic as it should be.
For the rest of you whining about XHTML.
Get with the program.
If I was hiring you and looked at your code (especially old timer guy) I would not hire you.
Maybe go back to Frontpage.
Wicked post.
Jquery rules!
Nice code. Jquery rocks.
I was about to apply that trick to my portfolio page, http://www.fedmich.com/works/ but in the end I just simply changed doctype, its much easier that way
Cheers to all :)
I agree with Jason Barnabe. Don't use a new window.
The standards way of opening up a popup window is to use a floating div.
Jason
"Any benefits received from using XHTML are offset by the fact that the page won't work for 80% of users out there.
"
Jason, I disagree with you on that. Most browsers support xhtml.
I find it funny that someone would say not to use a new window and use a floating div instead.
What happens if it is an offsite link?
Do you want your clients site users going to other sites and not returning?
Bringing standards into this is a poor excuse.