]> git.mjollnir.org Git - moodle.git/commitdiff
New function to make it easy when you just want to call one JavaScript function,...
authortjhunt <tjhunt>
Fri, 19 Sep 2008 04:51:47 +0000 (04:51 +0000)
committertjhunt <tjhunt>
Fri, 19 Sep 2008 04:51:47 +0000 (04:51 +0000)
lib/weblib.php

index 8dadfbc3da93ccd48a5d0066ddee5590ef1e4bbd..d95dd75fb20a65bc116dfab4abc5eaf0ca390ca8 100644 (file)
@@ -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 .= '<script type="text/javascript">//<![CDATA[' . "\n";
+    $html .= $function . '(' . implode(', ', $quotedargs) . ");\n";
+    $html .= "//]]></script>\n";
+    if ($return) {
+        return $html;
+    } else {
+        echo $html;
+    }
+}
 
 /**
  * Debugging aid: serve page as 'application/xhtml+xml' where possible,