Created
March 24, 2011 12:57
-
-
Save think49/885009 to your computer and use it in GitHub Desktop.
textareaSelectEndPoint.js : テキストボックスにフォーカスした時、文字列の最後にカーソルが移動させる。(IE8 はデフォルトでカーソルが先頭に移動する)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* textareaSelectEndPoint.js | |
* | |
* @version 1.0.4 | |
* @author think49 | |
* @url https://gist.github.com/885009 | |
*/ | |
(function () { | |
function handleLoad (event) { | |
var doc, inputs, input, textareas, i, len; | |
doc = event.target || document; | |
inputs = doc.querySelectorAll ? doc.querySelectorAll('input[type="text"]') : doc.getElementsByTagName('input'); | |
textareas = doc.getElementsByTagName('textarea'); | |
for (i = 0, len = inputs.length; i < len; ++i) { | |
input = inputs[i]; | |
if (input.type === 'text') { | |
addEvent(input, 'select', handleFocus, false); | |
} | |
} | |
for (var i = 0, l = textareas.length; i < l; i++) { | |
addEvent(textareas[i], 'focus', handleFocus, false); | |
} | |
} | |
function handleFocus (event) { | |
var target, range, length; | |
target = event.target || event.srcElement; | |
if (target.tagName === 'INPUT' && target.type === 'text' || target.tagName === 'TEXTAREA') { | |
console.log(event.type); | |
if ('setSelectionRange' in target) { // GoogleChrome5, Opera10.60, Firefox3.6.6 | |
// console.log('selectionStart'); | |
length = target.value.length; | |
target.setSelectionRange(length, length); | |
} else if ('createTextRange' in target) { // IE8 | |
// console.log('createTextRange'); | |
range = target.createTextRange(); | |
range.move('character', target.value.length); | |
range.select(); | |
} | |
} | |
} | |
function handleUnload () { | |
var caches, i, len; | |
caches = listenersCache.getAll(); | |
for (i = 0, len = caches.length; i < len; ++i) { | |
removeEvent.apply(caches[i]); | |
} | |
listenersCache.clearAll(); | |
// alert('unload'); | |
} | |
var listenersCache = (function () { | |
var caches; | |
caches = []; | |
return { | |
getAll: function getAll () { | |
return caches; | |
}, | |
set: function set (node, type, listener, useCapture) { | |
return caches.push([node, type, listener, useCapture]); | |
}, | |
clearAll: function clearAll () { | |
caches = null; | |
} | |
}; | |
})(); | |
var addEvent = (function () { | |
if (typeof addEventListener === 'function') { | |
return function (node, type, listener, useCapture) { | |
listenersCache.set(node, type, listener, useCapture); | |
return node ? node.addEventListener(type, listener, useCapture) : addEventListener(type, listener, useCapture); | |
}; | |
} | |
if (typeof attachEvent === 'object' || typeof attachEvent === 'function') { | |
return function (node, type, handleEvent) { | |
return node ? node.attachEvent('on' + type, handleEvent) : attachEvent('on' + type, handleEvent); | |
}; | |
} | |
throw new Error('addEvent is not defined'); | |
})(); | |
var removeEvent = (function () { | |
if (typeof removeEventListener === 'function') { | |
return function (node, type, listener, useCapture) { | |
return node ? node.removeEventListener(type, listener, useCapture) : removeEventListener(type, listener, useCapture); | |
}; | |
} | |
if (typeof detachEvent === 'object' || typeof detachEvent === 'function') { | |
return function (node, type, handleEvent) { | |
return node ? node.detachEvent('on' + type, handleEvent) : detachEvent('on' + type, handleEvent); | |
}; | |
} | |
throw new Error('removeEvent is not defined'); | |
})(); | |
// --- Initialize | |
if (typeof document.addEventListener === 'function') { | |
document.addEventListener('DOMContentLoaded', handleLoad, false); | |
listenersCache.set(document, 'DOMContentLoaded', handleLoad, false); | |
} else if (typeof attachEvent === 'object' || typeof attachEvent === 'function') { | |
attachEvent('onload', handleLoad); | |
listenersCache.set(null, 'load', handleLoad); | |
} | |
addEvent(null, 'unload', handleUnload, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment