drupal_to_js

Versions
5 – 6
drupal_to_js($var)

Converts a PHP variable into its Javascript equivalent.

We use HTML-safe strings, i.e. with <, > and & escaped.

Related topics

▾ 8 functions call drupal_to_js()

drupal_get_js in drupal/includes/common.inc
Returns a themed presentation of all JavaScript code for the current page. References to JavaScript files are placed in a certain order: first, all 'core' files, then all 'module' and finally all 'theme' JavaScript…
drupal_to_js in drupal/includes/common.inc
Converts a PHP variable into its Javascript equivalent.
profile_admin_settings_autocomplete in drupal/modules/profile/profile.module
Retrieve a pipe delimited string of autocomplete suggestions for profile categories
profile_autocomplete in drupal/modules/profile/profile.module
Callback to allow autocomplete of profile text fields.
taxonomy_autocomplete in drupal/modules/taxonomy/taxonomy.module
Helper function for autocompletion
update_do_update_page in drupal/update.php
Perform updates for the JS version and return progress.
upload_js in drupal/modules/upload/upload.module
Menu-callback for JavaScript-based uploads.
user_autocomplete in drupal/modules/user/user.module
Retrieve a pipe delimited string of autocomplete suggestions for existing users

Code

drupal/includes/common.inc, line 1847

<?php
function drupal_to_js($var) {
  switch (gettype($var)) {
    case 'boolean':
      return $var ? 'true' : 'false'; // Lowercase necessary!
    case 'integer':
    case 'double':
      return $var;
    case 'resource':
    case 'string':
      return '"'. str_replace(array("\r", "\n", "<", ">", "&"),
                              array('\r', '\n', '\x3c', '\x3e', '\x26'),
                              addslashes($var)) .'"';
    case 'array':
      // Arrays in JSON can't be associative. If the array is empty or if it
      // has sequential whole number keys starting with 0, it's not associative
      // so we can go ahead and convert it as an array.
      if (empty ($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
        $output = array();
        foreach ($var as $v) {
          $output[] = drupal_to_js($v);
        }
        return '[ '. implode(', ', $output) .' ]';
      }
      // Otherwise, fall through to convert the array as an object.
    case 'object':
      $output = array();
      foreach ($var as $k => $v) {
        $output[] = drupal_to_js(strval($k)) .': '. drupal_to_js($v);
      }
      return '{ '. implode(', ', $output) .' }';
    default:
      return 'null';
  }
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.