21.9. How to use Javascript in PHP-Nuke modules

You can include Javascript code in modules without problems in PHP-Nuke. You can proceed in two ways:

21.9.1. Javascript functions in javascript.php

PHP-Nuke come with its own Javascript functions included in the file javascript.php, located under the includes folder. You can add your own Javascript code there, if you follow some simple rules:

If you use vi (see Section 11.1), the following sequence of vi commands, applied to a Javascript file, will produce the right PHP code to append in the javascript.php file:

1,$s/"/\\"/g (1)
1,$s/^/echo "/ (2)
1,$s/$/";/ (3)
1,$s/";$/\\n";/ (4)
(1)
Escape all double quotes.
(2)
Add 'echo "' at the start of every line.
(3)
Add '";' at the end of every line.
(4)
Add a newline '\n' at the end of each Javascript line.

You can then include calls to your Javascript functions in the HTML code of your module. You echo the Javascript code, just like any other HTML code.

21.9.2. Complete Javascript code in modules

You can use the following template to include your complete Javascript code in your PHP-Nuke module:

<?php
if (!eregi("modules.php", $PHP_SELF)) {
  die ("You can't access this file directly...");
}
require_once("mainfile.php");
// 0 : do not show right blocks - 1:show right blocks
$index = 0; 
include("header.php");
?>
// PUT HTML OR JAVASCRIPT CODE HERE, 
// OR GET RID OF THE ?> AND <?php ABOVE AND BELOW THIS LINE 
// AND USE <acronym>PHP</acronym> CODE HERE INSTEAD.
<?php
include("footer.php");
?>

TipJavascript with PHP
 

If you decide to use PHP code ("echo"s) to enter your Javascript code, you should take into account the rules presented in Section 21.9.1.

TipJavascript codebase
 

You may need to add the following line to your Javascript applet:

codebase="modules/modulename"

Notes

[1]

the “window.open” line in the example above is actually a long line that has been broken to 3 lines only for layout purposes, that's why there are no newlines (\n) at the ends of two lines (just as there is no semicolon for the same reason).