Skip to content

Instantly share code, notes, and snippets.

@BigAB
Forked from remy/gist:330318
Created July 30, 2010 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigAB/501124 to your computer and use it in GitHub Desktop.
Save BigAB/501124 to your computer and use it in GitHub Desktop.
Clunky Edit for IE
/**
* Add this script to the end of your document that use <input autofocus type="text" />
* or <input type="text" placeholder="username" /> and it'll plug support for browser
* without these attributes
* Minified version at the bottom
*/
(function () {
function each(list, fn) {
var l = list.length;
for (var i = 0; i < l; i++) {
if (fn.call(list[i], list[i], i, list) === false) {
break;
}
}
}
var addEvent = (function () {
if (document.addEventListener) {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.addEventListener(type, fn, false);
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
} else {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
}
})();
var DomToArray = function(DOMobj) {
var arr = [];
for (var i in DOMobj) {
if (DOMobj[i].nodeType == 1) { arr.push(DOMobj[i]); }
}
return arr;
}
var i = document.createElement('input');
try {
var inputs = [].slice.call(document.getElementsByTagName('input'), 0);
}
catch(err) {
var inputs = DomToArray(document.getElementsByTagName('input'));
}
try {
inputs = inputs.concat([].slice.call(document.getElementsByTagName('textarea'), 0));
}
catch(err) {
inputs = inputs.concat(DomToArray(document.getElementsByTagName('textarea')));
}
if (!('placeholder' in i)) {
// placeholder fix
each(inputs, function (el) {
// note - we're using el instead of this across the board because it compresses better
var lastValue = el.value, placeholder = el.getAttribute('placeholder') ? el.getAttribute('placeholder') : '';
var focus = function () {
if (el.value == placeholder) {
el.value = '';
el.style.color = '';
}
};
var blur = function () {
if (el.value == '') {
el.value = placeholder;
el.style.color = '#A29797';
}
};
addEvent(el, 'focus', focus);
addEvent(el, 'blur', blur);
// remove the placeholder if the page is reload or the form is submitted
addEvent(el.form, 'submit', function () { focus.call(el); });
addEvent(window, 'unload', function () { focus.call(el); });
// set the default state
if (el.value == '') {
blur.call(el);
}
});
}
if (!('autofocus' in i)) {
// auto focus
each(inputs, function (el) {
if (el.getAttribute('autofocus') != null) {
el.focus();
return false; // "there can only be one"
}
});
}
})();
// minified
(function(){function f(k,j){var g=k.length;for(var h=0;h<g;h++){if(j.call(k[h],k[h],h,k)===false){break}}}var d=(function(){if(document.addEventListener){return function(k,j,h){if(k&&k.nodeName||k===window){k.addEventListener(j,h,false)}else{if(k&&k.length){for(var g=0;g<k.length;g++){d(k[g],j,h)}}}}}else{return function(k,j,h){if(k&&k.nodeName||k===window){k.attachEvent("on"+j,function(){return h.call(k,window.event)})}else{if(k&&k.length){for(var g=0;g<k.length;g++){d(k[g],j,h)}}}}}})();var e=function(j){var g=[];for(var h in j){if(j[h].nodeType==1){g.push(j[h])}}return g};var b=document.createElement("input");try{var a=[].slice.call(document.getElementsByTagName("input"),0)}catch(c){var a=e(document.getElementsByTagName("input"))}try{a=a.concat([].slice.call(document.getElementsByTagName("textarea"),0))}catch(c){a=a.concat(e(document.getElementsByTagName("textarea")))}if(!("placeholder" in b)){f(a,function(i){var h=i.value,k=i.getAttribute("placeholder")?i.getAttribute("placeholder"):"";var g=function(){if(i.value==k){i.value="";i.style.color=""}};var j=function(){if(i.value==""){i.value=k;i.style.color="#A29797"}};d(i,"focus",g);d(i,"blur",j);d(i.form,"submit",function(){g.call(i)});d(window,"unload",function(){g.call(i)});if(i.value==""){j.call(i)}})}if(!("autofocus" in b)){f(a,function(g){if(g.getAttribute("autofocus")!=null){g.focus();return false}})}})();
@BigAB
Copy link
Author

BigAB commented Jul 30, 2010

Please suggest improvements... I think a try catch is a bad hack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment