instance method String#extractScripts

View source on GitHub →

String#extractScripts() → Array

Extracts the content of any <script> blocks present in the string and returns them as an array of strings.

This method is used internally by String#evalScripts. It does not evaluate the scripts (use String#evalScripts to do that), but can be usefull if you need to evaluate the scripts at a later date.

Examples
'lorem... <script>2 + 2</script>'.extractScripts();
// -> ['2 + 2']
 '<script>2 + 2</script><script>alert("hello world!")</script>'.extractScripts();
// -> ['2 + 2', 'alert("hello world!")']
Notes

To evaluate the scripts later on, you can use the following:

var myScripts = '<script>2 + 2</script><script>alert("hello world!")</script>'.extractScripts();
// -> ['2 + 2', 'alert("hello world!")']
 var myReturnedValues = myScripts.map(function(script) {
  return eval(script);
});
// -> [4, undefined] (and displays 'hello world!' in the alert dialog)