MainelyDesign.com Blog

Using Jquery for target="_blank" and strict xHTML

Posted on 12/28/2009 at 03:27 pm by Kevin Wentworth
Viewed 15,329 times | 0 comments

If you like to see the little green check mark that HTML Validator shows when your HTML is 100% valid, you'll love this little trick. Thanks to badlydrawntoy.com

Target="_blank" Breaks xHTML Strict Guidelines

When you include the target attribute, your HTML will break the standards guideline for xHTML strict.  If you are using this doctype, you must use jquery to make your markup valid: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">.

Use jquery to open a new window (instead of target="_blank")

The following javascript code assumes you want all urls that start with http:// to open in a new window (target="_blank").  All relative links, /index.php, page.php, etc., won't be changed.  However, all href's that begin with http will be opened in a new window:

  1. $(document).ready( function() {
  2.     $('a[href^=http]').click( function() {
  3.         window.open(this.href);
  4.         return false;
  5.     });
  6. });

If you only want to target links with a class of external, <a class="external" ...> use the following code:

  1. $(document).ready( function() {
  2.     $('a.external').click( function() {
  3.         window.open(this.href);
  4.         return false;
  5.     });
  6. });

If you want to use this method but use the rel="" attribute, check out this post.

Cheers,
-Kevin Wentworth

Bookmark and Share

Tags for Using Jquery for target="_blank" and strict xHTML

Jquery | Tutorial | Example | Web Design | Hack

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design