Created
April 18, 2013 08:44
-
-
Save meghuizen/5411233 to your computer and use it in GitHub Desktop.
Escape HTML attributes using JavaScript
This file contains 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
/** | |
* Escapes HTML attributes using JavaScript | |
*/ | |
function escapeHtmlAttr(attr) { | |
var cdiv = document.createElement('div'); | |
cdiv.setAttribute('abc', attr); | |
return cdiv.outerHTML | |
.substr(10, cdiv.outerHTML.length - 18); | |
//.replace(/^<div abc="/i, '') | |
//.replace(/"><.div>$/i, ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment