useful jquery code snippets
Jul 26, 2009
i've seen these questions asked many sites, so i've decided to collect them in here. someone may find them useful. some simple and useful code pieces.open link in new window
xhtml strict doesn't allow you to usetarget="_blank" in links which opens the links in new windows. the solution to that problem is;
$(document).ready(function() {
$('a[href^="http"]').attr({target:'_blank'});
});
remove the iframe(aka digg bar)
everyone hates toolbars like digg bar, or facebook's bar. here's a simple snippet to get rid of them.if(top !== self) top.location.href = self.location.href;
auto close a div after 5 seconds
well, of course it doesn't have to be exactly 5 seconds but, let's say after some seconds. this is usefull especially after some sort of a ajax request.
setTimeout(function() {
$('.mydiv').hide('blind', {}, 500)
}, 5000);
e-mail validation
useful in registration forms. i assume you don't only rely on javascript for form validation.
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var email = $.trim($('.email').val());
if(!emailReg.test(email)) {
alert('please type a valid e-mail address.');
return false;
}
auto focus to form input
this is also usefull for login forms.
function focus_form() {
document.getElementsByTagName('input')[0].focus();
}
window.onload = focus_form;
and also there's a great website for jquery which lists examples on every topic.
http://www.jquerylist.com/