Jquery Snippets By: Programmers Help
Jquery Snippets By: Programmers Help
By
Programmers help
Programmers help
Contents
Contents................................................................................................................ 2
Print Page........................................................................................................... 3
Disabling Right Click........................................................................................... 3
Scroll to Top........................................................................................................ 3
jQuery Cookies.................................................................................................... 4
Replace text in a string....................................................................................... 5
Preloading Images.............................................................................................. 5
Image Resizing................................................................................................ 6
Clear Form Data................................................................................................. 7
Prevent Multiple Submit...................................................................................... 7
Test Password Strength...................................................................................... 8
png fix for IE6..................................................................................................... 8
Parsing json with jQuery..................................................................................... 9
Alternating Row Colors..................................................................................... 10
Make entire div clickable..................................................................................10
check if an image has loaded...........................................................................10
Opening pop-ups.............................................................................................. 11
Partial Page Refresh......................................................................................... 11
Disable all links................................................................................................. 11
Disable Scrolling............................................................................................... 11
Programmers help
Print Page
<!-- jQuery: Print Page -->
$('a.printPage').click(function(){
window.print();
return false;
});
<!-- HTML: Print Page -->
<div>
<a class="printPage" href="#">Print</a>
</div>
Scroll to Top
$("a[href='#top']").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
Programmers help
jQuery Cookies
//get cookie
function getCookie( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ';', len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
//set cookie
function setCookie( name, value, expires, path, domain, secure ) {
var today = new Date();
today.setTime( today.getTime() );
if ( expires ) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name+'='+escape( value ) +
( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) +
//expires.toGMTString()
( ( path ) ? ';path=' + path : '' ) +
( ( domain ) ? ';domain=' + domain : '' ) +
( ( secure ) ? ';secure' : '' );
}
//delete cookie
function deleteCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + '=' +
( ( path ) ? ';path=' + path : '') +
( ( domain ) ? ';domain=' + domain : '' ) +
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
Programmers help
Preloading Images
(function($) {
var cache = [];
// Arguments are image paths relative to the current page.
$.preLoadImages = function() {
var args_len = arguments.length;
for (var i = args_len; i--;) {
var cacheImage = document.createElement('img');
cacheImage.src = arguments[i];
cache.push(cacheImage);
}
}
jQuery.preLoadImages("image1.gif", " image2.png");
Programmers help
Image Resizing
$(window).bind("load", function() {
// IMAGE RESIZE
$('#product_cat_list img').each(function() {
var maxWidth = 120;
var maxHeight = 120;
var ratio = 0;
var width = $(this).width();
var height = $(this).height();
if(width > maxWidth){
ratio = maxWidth / width;
$(this).css("width", maxWidth);
$(this).css("height", height * ratio);
height = height * ratio;
}
var width = $(this).width();
var height = $(this).height();
if(height > maxHeight){
ratio = maxHeight / height;
$(this).css("height", maxHeight);
$(this).css("width", width * ratio);
width = width * ratio;
}
});
//$("#contentpage img").show();
// IMAGE RESIZE
});
Programmers help
Programmers help
$('#pass').keyup(function(e)
{
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?
=.*\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?
=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
if (false == enoughRegex.test($(this).val())) {
$('#passstrength').html('More Characters');
} else if (strongRegex.test($(this).val())) {
$('#passstrength').className = 'ok';
$('#passstrength').html('Strong!');
} else if (mediumRegex.test($(this).val())) {
$('#passstrength').className = 'alert';
$('#passstrength').html('Medium!');
} else {
$('#passstrength').className = 'error';
$('#passstrength').html('Weak!');
}
return true;
});
Programmers help
Programmers help
1</div>
2</div>
3</div>
4</div>
The following lines of jQuery will make the entire div clickable:
$(".myDiv").click(function(){
window.location=$(this).find("a").attr("href");
return false;
});
Programmers help
Opening pop-ups
jQuery(a.popup).live(click, function()
{
newwindow=window.open($(this).attr(href),,height=100,width=100);
if(window.focus)
{
newwindow.focus()
}
return false;
});
Disable Scrolling
// Prevent Scrolling
$(document).bind("touchmove",function(event){
event.preventDefault();
});
Programmers help