Skip to content

Instantly share code, notes, and snippets.

@monolithed
Last active December 19, 2015 03:29
Show Gist options
  • Save monolithed/5890989 to your computer and use it in GitHub Desktop.
Save monolithed/5890989 to your computer and use it in GitHub Desktop.
// 0.
'<script><\/script>' // deprecated
'<' + 'script>' + '<' + '/script>' // trash
// 1.
var script = document.createElement('script');
script.textContent = 'var foo = 1';
var serializer = new XMLSerializer();
var string = serializer.serializeToString(script);
alert(string); // <script>var foo = 1</script>
// 2.
var escape_slash = function(value) {
return value.replace(/<|>/g, function(chars, value) {
return chars[value];
}
.bind(null, {
'<' : '\x3C',
'>' : '\x3E'
}));
};
alert(escape_slash('<script>var foo = 1</script>')); // <script>var foo = 1</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment