Use jQuery to make all offsite links open in a new window

XHTML Strict does not allow opening links in a new window using target=””_blank”” so instead you can use jQuery to add the target attribute to all <a> tags after the page has loaded and still have the page validate as XHTML strict. (But refer to my updated note at the end of the article regarding the base tag and the un-deprecation of the target attribute).

jQuery Code

Simply add the following piece of code into your $(document).ready(function() { } section of code (more about that in my jQuery’s document ready initialization post):

$(""a"").filter(function() {
    return this.hostname && this.hostname !== location.hostname;
}).attr('target', '_blank');

What this does is to add target=’_blank’ to all <a> tags in the current webpage that do not link to the domain you are currently on.

Therefore on my blog, applying the jQuery code above would make the following open in the same window:

<a href=""/jquery-open-offsite-links-new-window/""> ... </a>
<a href=""https://electrictoolbox.com/jquery-open-offsite-links-new-window/""> ... </a>

and the following in a new window:

<a href=""http://www.google.com""> ... </a>
<a href=""http://www.yahoo.com""> ... </a>

It’s then possible to replace attr() with other functions to do things to external links on a page. The next post shows how to add one of those offsite link images after the link with jQuery.

Update September 23rd 2011

With HTML5, the W3C has un-deprecated the target attribute so you can add target attributes to <a> and <form> tags and have the document still validate. Using jQuery as shown above can still be useful if you have a lot of pages and don’t wish to have to manually update all the links.

Opacity for Internet Explorer using CSS3 PIE and Alpha Transparency

Alpha transparency is a much easier way to make a background transparent than having to specify a whole bunch of cross browser CSS opacity statements and works in a wide variety of browsers, with the usual exception of Internet Explorer prior to version 9. If you are using the CSS3 PIE utility in your web project then there’s an easy way to use alpha transparency with CSS3 PIE.

What am I trying to achive?

I’ve used this technique myself for the top navigation of one of my websites where there’s a background image with navigation tabs on top. The website also uses border-radius which PIE also helps with, but I won’t include that in the example in this post.

The screenshot below shows an example of a background image with a transparent block on top with some text. The background image can be seen opaque through the block.

example of transparency using alpha transparency

The HTML and CSS

In the above example, the HTML looks like this:

<div id="exampleContainer">
 <div id="exampleTransparency">Example transparency</div>
 </div>
 

The CSS looks like this:

#exampleContainer {
 background: url(https://electrictoolbox.com/wp-content/uploads/running.jpg);
 height: 97px;
 width: 500px;
 }
 #exampleTransparency {
 /* set background with alpha channel transparancy: 1st for IE no PIE, 2nd for IE PIE, 3rd for others */
 background: rgb(95,83,75);
 -pie-background: rgba(0,0,0,0.5);
 background: rgba(0,0,0,0.5);
 /* add PIE for IE < 9 */
 behavior: url(https://electrictoolbox.com/examples/pie.htc);
 /* IE needs position set to something other than the default for this to work */
 position: relative;
 /* other css settings for content */
 color: white;
 padding: 10px;
 }
 /* prevent IE9 using PIE */
 :root *> #exampleTransparency {
 behavior: none;
 }
 
 

It’s fairly self explanatory but just a couple of points…

There are three declarations for the background. The first is the fallback which will show a solid background color if PIE doesn’t work (i.e. Javascript is switched off). The second is the declaration for PIE which will be ignored by all browsers other than IE using PIE. The third sets the background with alpha transparency. Browsers that don’t support this will ignore it and use the earlier declaration.

Obviously change the path for pie.htc to where you store it on your own site. It does need to have the full path to it, as I mentioned in my gotchas about PIE post.

And finally that odd looking declaration at the end prevents IE9 from using PIE at all; by doing this it ensures IE9 will not download the pie.htc file. I talked about this in an earlier post as well.

Browser support for alpha transparency

All modern browsers support alpha transparency:

  • Chrome
  • Firefox 3+
  • IE9+
  • Opera 10+
  • Safari 3+

Working example

I have a working example available to view in a browser, which should look more or less exactly like the screenshot example in this post.

Solving a couple of gotchas with CSS3 PIE

CSS3 PIE is a utility for Internet Explorer which adds support for some of the most useful CSS3 properties missing from older versions of the browser, such as border-radius, box-shadow, border-image and multiple background images. I don’t wish to duplicate what’s in the online documentation in this post, just a couple of gotchas when starting …

Read more

Inject HTML content into Disqus and jQuery

My last post looked at how to run Javascript functions after Disqus has loaded using their undocumented callback functions. The reason I needed to do this myself was to inject some additional content in the Disqus HTML, which I show how to do this in this. Update for Disqus 2012 (June 15th 2012) The disqus_config() …

Read more

PHP: Class XsltProcessor not found

A PHP install on Debian 5 by default does not include the Xslt extension which means the following error will occur if trying to use the XsltProcessor class: " Fatal error: Class ‘XsltProcessor’ not found…". This post shows how to install the extension on Debian 5.

Categories PHP