Skip to content

Instantly share code, notes, and snippets.

@owencjones
Created December 3, 2014 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owencjones/efd0ef664b69718399e8 to your computer and use it in GitHub Desktop.
Save owencjones/efd0ef664b69718399e8 to your computer and use it in GitHub Desktop.
Clean user input in Client-Side JS
/*
Depends on JQuery. By taking input and adding it as text to a DOM-injected div, it forces the content to go
through JQuery's in-built sanitation. Rendering it clean of DOM-injection attacks.
Taken from Socket.io's chat demo source.
https://github.com/Automattic/socket.io/blob/master/examples/chat/public/main.js#L148
*/
function cleanInput (input) {return $('<div/>').text(input).text();}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment