From bd6b79788294afb97c3a7287d6f5366752921c2b Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Tue, 5 Oct 2010 17:16:05 +0200 Subject: [PATCH] Some more optimizations thanks to my -ot homies --- jquery.placeholder.js | 38 +++++++++++++++++++------------------- jquery.placeholder.min.js | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/jquery.placeholder.js b/jquery.placeholder.js index 7a2033b..888d4b7 100644 --- a/jquery.placeholder.js +++ b/jquery.placeholder.js @@ -5,6 +5,13 @@ */ ;(function($) { + if ('placeholder' in document.createElement('input')) { + $.fn.placeholder = function() { + return this; + }; + return; + } + function args($elem) { // Get attributes string from outerHTML var html = $('
').append($elem.clone()).html().replace(/<(\w+)\s+(.*)>/, '$2'), @@ -29,9 +36,6 @@ } } - // Made this a function, because we actually need it on two different occasions: - // 1) Once when the DOM is loaded; - // 2) Once every time the focusout() is triggered. function setPlaceholder(elem) { var $replacement, $elem = $(this); @@ -52,10 +56,12 @@ } } - // Look for forms - $('form').bind('submit.placeholder', function() { - // Clear the placeholder values so they don’t get submitted - $('.placeholder', this).val(''); + $(function() { + // Look for forms + $('form').bind('submit.placeholder', function() { + // Clear the placeholder values so they don’t get submitted + $('.placeholder', this).val(''); + }); }); // Clear placeholder values upon page reload @@ -63,17 +69,11 @@ $('.placeholder').val(''); }); - if ('placeholder' in document.createElement('input')) { - $.fn.placeholder = function() { - return this; - }; - } else { - $.fn.placeholder = function() { - return this.filter(':input[placeholder]').bind({ - 'focus.placeholder': onFocus, - 'blur.placeholder': setPlaceholder - }).trigger('blur.placeholder').end(); - }; - } + $.fn.placeholder = function() { + return this.filter(':input[placeholder]').bind({ + 'focus.placeholder': onFocus, + 'blur.placeholder': setPlaceholder + }).trigger('blur.placeholder').end(); + }; })(jQuery); \ No newline at end of file diff --git a/jquery.placeholder.min.js b/jquery.placeholder.min.js index 033f5a5..a3e0ded 100644 --- a/jquery.placeholder.min.js +++ b/jquery.placeholder.min.js @@ -3,4 +3,4 @@ * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin * @author Mathias Bynens */ -(function(d){function a(f){var h=d('
').append(f.clone()).html().replace(/<(\w+)\s+(.*)>/,'$2'),e,g={};while((e=h.match(/\s*([\w\-]+)=("[^"]*"|'[^']*'|\w+)/))){g[e[1]]=e[2].replace(/^(["'])(.*?)\1$/,'$2');h=h.replace(e[0],'')}return g}function b(){var e=d(this);if(e.val()===e.attr('placeholder')&&e.hasClass('placeholder')){if(e.data('placeholder-password')){e.next().show().focus().end().remove()}else{e.val('').removeClass('placeholder')}}}function c(g){var i,f=d(this);if(f.val()===''||f.val()===f.attr('placeholder')){if(f.is(':password')){try{i=f.clone().attr({type:'text'})}catch(h){i=d('',d.extend(a(f),{type:'text'}))}i.data('placeholder-password',true).bind('focus.placeholder',b);f.hide().before(i);f=i}f.addClass('placeholder').val(f.attr('placeholder'))}else{f.removeClass('placeholder')}}d('form').bind('submit.placeholder',function(){d('.placeholder',this).val('')});d(window).bind('unload.placeholder',function(){d('.placeholder').val('')});if('placeholder' in document.createElement('input')){d.fn.placeholder=function(){return this}}else{d.fn.placeholder=function(){return this.filter(':input[placeholder]').bind({'focus.placeholder':b,'blur.placeholder':c}).trigger('blur.placeholder').end()}}})(jQuery); \ No newline at end of file +(function(d){if('placeholder' in document.createElement('input')){d.fn.placeholder=function(){return this};return}function a(f){var h=d('
').append(f.clone()).html().replace(/<(\w+)\s+(.*)>/,'$2'),e,g={};while((e=h.match(/\s*([\w\-]+)=("[^"]*"|'[^']*'|\w+)/))){g[e[1]]=e[2].replace(/^(["'])(.*?)\1$/,'$2');h=h.replace(e[0],'')}return g}function b(){var e=d(this);if(e.val()===e.attr('placeholder')&&e.hasClass('placeholder')){if(e.data('placeholder-password')){e.next().show().focus().end().remove()}else{e.val('').removeClass('placeholder')}}}function c(g){var i,f=d(this);if(f.val()===''||f.val()===f.attr('placeholder')){if(f.is(':password')){try{i=f.clone().attr({type:'text'})}catch(h){i=d('',d.extend(a(f),{type:'text'}))}i.data('placeholder-password',true).bind('focus.placeholder',b);f.hide().before(i);f=i}f.addClass('placeholder').val(f.attr('placeholder'))}else{f.removeClass('placeholder')}}d(function(){d('form').bind('submit.placeholder',function(){d('.placeholder',this).val('')})});d(window).bind('unload.placeholder',function(){d('.placeholder').val('')});d.fn.placeholder=function(){return this.filter(':input[placeholder]').bind({'focus.placeholder':b,'blur.placeholder':c}).trigger('blur.placeholder').end()}})(jQuery); \ No newline at end of file