Skip to content

Instantly share code, notes, and snippets.

@qhoxie
Forked from newtonapple/gist:671669
Created November 10, 2010 23:12
Show Gist options
  • Save qhoxie/671704 to your computer and use it in GitHub Desktop.
Save qhoxie/671704 to your computer and use it in GitHub Desktop.
// non-id/name attributes are interpreted as cdata
// per: http://www.w3.org/TR/html4/types.html#type-cdata
var s = "<p title=\"&lt;script&gt;alert('TEST')&lt;/script&gt;\">testing 123</p>";
document.body.innerHTML = s;
document.getElementsByTagName('p')[0].getAttribute('title');
// => "<script>alert('TEST')</script>"
document.getElementsByTagName('p')[0].title;
// => "<script>alert('TEST')</script>"
// there is no way to distinguish, on read, any permutation of encoded or non-encoded values
var s = "<p data-foo=\"\<script\>alert('TEST')&lt;/script&gt;\">testing 123</p>";
document.body.innerHTML = s;
document.getElementsByTagName('p')[0].getAttribute('data-foo');
// => "<script>alert('TEST')</script>"
var s = '<p data-foo="' + "<script>alert('TEST')</script>" + '">testing 123' + '</p>';
document.body.innerHTML = s;
document.getElementsByTagName('p')[0].getAttribute('data-foo');
// => "<script>alert('TEST')</script>"
// setting and reading in-line yields the entities intact
var s = "<p>testing 123</p>";
document.body.innerHTML = s;
document.getElementsByTagName('p')[0].setAttribute('data-foo', "&lt;script&gt;alert('TEST')&lt;/script&gt;");
document.getElementsByTagName('p')[0].getAttribute('data-foo');
// => "&lt;script&gt;alert('foo')&lt;/script&gt;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment