Category: JavaScript

Using JQuery To Open External Links In A New Window

19 February, 2010 | JQuery | No comments

Opening external links in a new window can be useful, but adding target="_blank" can be a real chore. Not only that, but if you are trying to validate the page to XHTML strict then the target attribute will cause errors to appear as it is not defined in XHTML.

An easy solution to this issue is to add the following JQuery to your site. It will look for any links that start with http and do not contain the current domain and add a new click event to them that causes a new window to be opened. This will exclude most links straight away as they will most likely be relative.

$("a[href^='http']:not([href*='example.com'])").click(function(){  window.open(this.href);  return false; }).attr("title", "Opens in a new window");

This code will also add a new title to each link, replacing any that already exist, just remove the call to attr() at the end of the block to stop this. If you are using this on your site then make sure you put this into a dom ready block, and also that you change the example.com to be your own domain name.

Written by Philip Norton.

Scroll To First Error Message On Page With jQuery And ScrollTo

29 January, 2010 | JQuery | 1 comment

If you have a large page or form that uses validation on it then you will probably want to tell the user that something is going on. One way to do this is by telling the user at the top of the page that something has gone wrong and then letting them figure out where.

A more elegant solution is to scroll the page down the just above the first error message so that the user is aware of what they need to fill in. This can easily be done through a combination of jQuery and the ScrollTo plugin.

The first thing you need on the form is add some tags as the error output elements. Make sure that these tags don't get printed if there are no errors. I have chosen to use p tags with the class of error. Download this plugin and include it within your page.

<p class="error">There was a problem with this element.</p>

All that is needed now is to tell ScrollTo that we want to scroll to the first error message on the screen. We can do this by using the filter "p.error:1" which will find the first p tag with a class of error on the page. You could also use the ":first" filter instead of this, but both have the same outcome.

This gets passed to the ScrollTo plugin in the following way.

$.scrollTo($('p.error:1'));

The slight problem here is that this scrolls the page down so that this element is flush with the top of the page, which makes sense if the error is above the element. If the error is below the element then you will need to set and offset so that there is enough space between the top of the page and the error message to fit the form element in. This is done through use of the offset option and a negative value, like this.

$.scrollTo($('p.error:1'), {offset: {top:-200}});

The good thing about this is that you can add to any form quite easily and it will have no effect unless there is a paragraph tag with a class of error.

Written by Philip Norton.

JQuery Image Switcher

7 December, 2009 | JQuery | No comments

Using something like CrossSlide is fine if you want fancy effects in your image translations, but for more simple effects you can use a single function to simply swap one image for another. First, lets create some simple markup that will allow us to display some images.

<div class="slideshow"> <img src="image1.png" alt="Image 1" /> <img src="image2.png" alt="Image 2" /> <img src="image3.png" alt="Image 3" /> <img src="image4.png" alt="Image 4" /> </div> (more...)

Written by Philip Norton.

JavaScript Text Length Tool

4 September, 2009 | JavaScript | No comments

I quite often find myself needing to know how long a string is, especially when testing form validation or when trying to write a page description. I therefore like to have this little tool to help me by simply counting the number of characters in a given string.

Use the JavaScript text length tool here.

The tool uses a single form with a textarea and a text box. The textarea has onkeyup and onkeydown events which causes it to call a function that counts the number of characters and enters this into the text box.

<form onsubmit="return false;" action="" id="textLengthCalc"> <textarea name="textarea" rows="10" cols="68" onkeydown="textCounter()" onkeyup="textCounter()"></textarea> <input readonly="readonly" size="15" type="text" name="len" maxlength="10" value="0" /> </form>

The following is the JavaScript function that is called when anything is put into the textarea. The function finds the form and then trims any white space from the textarea's contents before entering how long the string is into the text box.

function textCounter(){  var area = document.getElementById('textLengthCalc');  area.len.value = area.textarea.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length; }

A simple bit of CSS styling makes the tool slightly more usable.

Written by Philip Norton.

JavaScript Confirm Action Dialog

2 September, 2009 | JavaScript | No comments

If you want to create a link that performs an action that can't be rolled back then you might want to stop the user from clicking that link unless they really want to. The best way to do this is to intercept the link with a confirm() command.

The first way to do this (and especially useful if you want to ad other functionality) is to use the following function.

function confirmDelete(delUrl) {  if (confirm("Are you sure you want to delete")) {   document.location = delUrl;  } }

For each link that you want to use this function on just replace the href with a simple bookmarklet like the following, pass the URL you want to use within this function call.

<a href="javascript:confirmDelete('delete.page?id=1')">Delete</a>

Another way is to use the href attribute in the usual way, but to add an onclick event that intercepts the user clicking on the link. The confirm() function returns true or false if the user clicks ok or cancel respectively. With the addition of the return the onclick command will follow the link if the user clicks ok.

<a href="delete.page?id=1" onclick="return confirm('Are you sure you want to delete?')">Delete</a>

This is an accessible way of creating a system, especially if you know that some of your users will not be using JavaScript. However, you should be very aware when using this functionality that some bad things can happen. For example, if a search engine happens to come across a page with these link on it will click them all and you will find data in your system disappearing! This is a known gotcha and the only way around it is to simply use a button to run any action that destructs data.

Written by Philip Norton.