From: tjhunt Date: Fri, 19 Sep 2008 04:51:47 +0000 (+0000) Subject: New function to make it easy when you just want to call one JavaScript function,... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=78b5eb25c8e9974ed7e2cce02de13fe8c15b3d32;p=moodle.git New function to make it easy when you just want to call one JavaScript function, so you don't have to worry about constructing the script tag yourself. I did this in the context of MDL-12391, but I think it is generally userful. --- diff --git a/lib/weblib.php b/lib/weblib.php index 8dadfbc3da..d95dd75fb2 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2725,6 +2725,38 @@ function get_require_js_code($loadlibs) { return $output; } +/** + * Generate the HTML for calling a javascript funtion. You often need to do this + * if you have your javascript in an external file, and need to call one function + * to initialise it. + * + * You can pass in an optional list of arguments, which should be strings or + * numbers. Numeric arguments are used unmodified. String arguments automatically + * have addslashes_js called on them, and are wrapped in quotes for you. + * + * @param string $function the name of the JavaScript function to call. + * @param array $args an optional list of arguments to the function call. + * @param boolean $return if true, return the HTML code, otherwise output it. + */ +function print_js_call($function, $args = array(), $return = false) { + $quotedargs = array(); + foreach ($args as $arg) { + if (is_number($arg)) { + $quotedargs[] = $arg; + } else { + $quotedargs[] = "'" . addslashes_js($arg) . "'"; + } + } + $html = ''; + $html .= '\n"; + if ($return) { + return $html; + } else { + echo $html; + } +} /** * Debugging aid: serve page as 'application/xhtml+xml' where possible,