JSON Escaped String

Categories

Component ID

1944386

Component name

JSON Escaped String

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Component created

Component changed

Component body

Drupal uses php's json_encode() to convert php arrays and objects into javascript's object literal syntax. The JSON specification however is much more narrow than the full breadth of object literals; in particular, values are more or less limited to strings and numbers. This disallows expressions, anonymous functions, and even variable references.

This module provides a method to unquote values sent to Drupal.settings! To use simply do something like this:

drupal_add_js(array('myModule' => array(
  'onStart' => new json_escaped_string('Drupal.myModule.startCallback'),
  'onFinish' => new json_escaped_string('
    function () {
      console.log("finished");
    }
  '),
)), 'setting');