External Links New Window

Open All External Links in a New Browser Window

Expanding on the post about XHTML valid target post.

Lets make this a bit automated using jQuery to search for external links and add a class named external.
As before, these links will be XHTML valid.
Then from the post we are expanding on these links will be automatically opened in a new window.

To start we need to load jQuery in our site, quick and simple we will load it from Google.

Moving on to the code needed for our task,
The Full Code, I will explain following the code.

The line that MUST BE CHANGED

The http://localhost/ needs to be changed to the URL of your site.
For example, here it would be http://validwebs.com/

It gets added to the urlcheck variable and used for checking all links against with in the viewed page.
Then the search happens in this line

If any links in the page do not have the URL var local they will have the external class added to them.

Then the click function is added to the class external the same as in the original post.

From that point on all external links with in the site will open in a new browser window.