Last active
August 29, 2015 13:56
-
-
Save ashi009/8972596 to your computer and use it in GitHub Desktop.
htc script based placeholder polyfill for IE9
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
<html> | |
<head> | |
<link href="placeholder-fix.css" rel="stylesheet"> | |
</head> | |
<body> | |
<div class="Control"> | |
<input type="text" placeholder="123"> | |
</div> | |
</body> | |
</html> |
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
.Control { | |
display: inline-block; | |
position: relative; | |
} | |
input, textarea { | |
behavior: url("placeholder-fix.htc")\9; | |
} | |
.Placeholder { | |
position: absolute; | |
left: 0; | |
top: 0; | |
right: 0; | |
} |
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
<PUBLIC:PROPERTY NAME="elPlaceholder" /> | |
<PUBLIC:ATTACH EVENT="oncontentready" ONEVENT="generate()" /> | |
<PUBLIC:ATTACH EVENT="onkeyup" ONEVENT="update()" /> | |
<PUBLIC:ATTACH EVENT="onchange" ONEVENT="update()" /> | |
<PUBLIC:ATTACH EVENT="onresize" ONEVENT="updateStyle()" FOR="window" /> | |
<SCRIPT LANGUAGE="JScript"> | |
function generate() { | |
var content = element.getAttribute('placeholder'); | |
if (!content) | |
return; | |
elPlaceholder = document.createElement('span'); | |
elPlaceholder.className = 'Placeholder'; | |
elPlaceholder.appendChild(document.createTextNode(content)); | |
updateStyle(); | |
element.parentNode.insertBefore(elPlaceholder, element.nextSibling); | |
elPlaceholder.addEventListener('click', function(evt) { | |
element.focus(); | |
evt.preventDefault(); | |
evt.stopPropagation(); | |
}, false); | |
} | |
function updateStyle() { | |
var computedStyle = window.getComputedStyle(element); | |
var style = elPlaceholder.style; | |
var scrollbarWidth = element.offsetWidth - element.clientWidth; | |
if (scrollbarWidth) | |
style.right = scrollbarWidth + 'px'; | |
style.paddingLeft = computedStyle.paddingLeft; | |
style.paddingRight = computedStyle.paddingRight; | |
style.paddingTop = computedStyle.paddingTop; | |
style.lineHeight = computedStyle.lineHeight; | |
style.fontSize = computedStyle.fontSize; | |
} | |
function update() { | |
elPlaceholder.style.display = element.value ? 'none' : 'inherit'; | |
} | |
</SCRIPT> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment