Last active
January 17, 2022 22:02
-
-
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
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
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