Skip to content

Instantly share code, notes, and snippets.

@rhysburnie
Last active January 17, 2022 22:02
Show Gist options
  • Save rhysburnie/5360796 to your computer and use it in GitHub Desktop.
Save rhysburnie/5360796 to your computer and use it in GitHub Desktop.
jQuery ajax dataFilter to remove scripts from html string to prevent script execution if loading string into a temp dom element
function dfStripScripts = function(data, type)
{
// incase the response is full html with scripts remove them
type = type || 'text';
if(type=='html'||type=='text'){
/*return data.replace(/<script.*>.*?<\/script>/gi, '');*/
return data.replace(/<script.*?>([\w\W\d\D\s\S\0\n\f\r\t\v\b\B]*?)<\/script>/gi, '');
}
return data;
};
/**
* Example:
*
* $.ajax('a-url', {
* dataFilter: dfStripScripts
* // other stuff
* });
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment