Jump to content
blogsmith

How to make target="_blank" pass W3C validation

Recommended Posts

I received a suggestion as follows - between dotted line...............................You can still use XHTML strict and target hyperlinks and yes still have it pass with W3C validation. You can do this with Javascript between your heading tags you place this code:$(function(){$('a[rel=external]').attr('target','_new');});Then in every link you want to target the new window add rel="external" to the anchor. That will reference the javascript function and open it in a new window and all of which will validate in W3C as XHTML strict. Hope this helps. ..............................I will like to add more codes to the above so that I can test it at http://validator.w3.org/Will this do?................................<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html dir='ltr' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'> <head> <a href="http://www.w3schools.com" target="_blank" rel="external">W3Schools</a></head></html>............................If not how can I make the above complete enough to submit for validation?

Share this post


Link to post
Share on other sites

sorry, maybe like this:................................<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html dir='ltr' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'> <head> </head><a href="http://www.w3schools.com" target="_blank" rel="external">W3Schools</a></html>............................

Share this post


Link to post
Share on other sites

Well, if you use the JavaScript you can remove the target attribute, as the script adds it back in dynamically.

Share this post


Link to post
Share on other sites
Well, if you use the JavaScript you can remove the target attribute, as the script adds it back in dynamically.
I tested adding the script just before </head> for my test blog athttp://testing-blogger-beta.blogspot.com/specifically athttp://testing-blogger-beta.blogspot.com/2...new-window.htmland made this link<a href="http://www.bloggertipsandtricks.com" rel="external">Blogger Tips and Tricks</a>but the link still open in the same window?

Share this post


Link to post
Share on other sites

Maybe you want to make it set the value of the target attribute to _blank, not _new?P.S. target is deprecated for a reason...

Share this post


Link to post
Share on other sites

From what i can see the code is not within any script tags as in <script type="text/javascript">$(function(){$('a[rel=external]').attr('target','_new');});</script>I also don't any links to jquery libraries files at all.

Share this post


Link to post
Share on other sites

Adding the target attribute with Javascript is just cheating against the validator.For what it's worth, you could also be creating font and center tags with the DOM methods and the validator would still consider it correct.If you're going to use Javascript, you might as well do it correctly. Loop through the links and bind an onclick even which runs the window.open() method instead.But if you really absolutely need the target attribute, just switch to a transitional DTD.

Share this post


Link to post
Share on other sites

I like to use the following method as it degrades nicely if a popup blocker is being used.

<a href="some url" rel="external">My Link</a>...<script type="text/javascript">var links = document.getElementsByTagName("a");for(var i=0;i<links.length;i++) {  if(links[i].getAttribute('rel') == 'external') {	links[i].onclick = function() {	  return !window.open(this.href);	};  }}</script>

What that click handler does is try to open the link in a new windows. If it fails, fior whatever reason, it will then open the link in the current window. This way if the user has a popup blocker they can still access the link.

Edited by aspnetguy

Share this post


Link to post
Share on other sites
From what i can see the code is not within any script tags as in <script type="text/javascript">$(function(){$('a[rel=external]').attr('target','_new');});</script>I also don't any links to jquery libraries files at all.
Exactly right. That bit of Javascript is jQuery, so you need to link to the jQuery library.The target attribute is coming back in HTML5, so you could use that, but bear in mind the lack of consistent support for HTML5 features across all browsers.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...