How to use Javascript in PHP-Nuke blocks

20.5. How to use Javascript in PHP-Nuke blocks

Javascript can be used with PHP-Nuke without problems, as long as you remember that they operate in different environments (see Frames, JavaScript, and PHP Overview):

  • PHP-Nuke, like PHP, is server side. Therefore, if you want to pass data from a form to PHP-Nuke, you have to submit it and load the page again. There is no getting around it.

  • Javascript is client side (run in the browser), and can help you with dynamic functionality. You can easily use JavaScript and PHP-Nuke together. However, just like PHP, PHP-Nuke knows nothing about Javascript. PHP-Nuke will just echo the Javascript code to the HTML page, just as it would echo the value of a string variable.

Thus, from the point of view of PHP and PHP-Nuke, there is no difference between

<?php
echo 'Welcome to PHP-Nuke';
?>

and

<?php
echo '<script type="text/javascript">
<!--
if ( 1 ) {
    window.open(\'news.html\', \'_WELCOME\',
    \'HEIGHT=450, resizable=yes, WIDTH=600\');
}
//-->
</script>';
?>

They both echo strings, of which the first one is simply displayed in the client's browser as a welcome message, while the second one is interpreted by the client's browser and leads to the browser sending a request for the News module, which in turn sends the usual home page of a PHP-Nuke site to be displayed on the client.

With this in mind, Javascript does not demand any special treatment when used in PHP-Nuke blocks. Just append the Javascript code to the $content variable of the block and you are done. Of course, you have to escape double quotes - and it is a good idea to add a newline (\n) to the end of each Javascript line (see Javascript and BLOCKs).

If you use sed, the following sequence of sed commands, applied to a Javascript file, will produce the right PHP code to include in the PHP-Nuke block:

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

You can put them in a file, sedscr, and tell sed to read sedscr for commands, while editing the original Javascript file (javascript):

sed -f sedscr javascript > block-Javascript.php

Of course, you can use vi (Section 11.1) too - see Section 21.9.1 for a similar example - or just do the substitutions manually. The examples in this section demonstrate what you have to do (see also Javascript in a PHP-Nuke block):

 

Site Info v2.2.2

Last SeenLast Seen
Server TrafficServer Traffic
  • Total: 342,095,219
  • Today: 44,534
Server InfoServer Info
  • Nov 18, 2017
  • 03:12 pm PST
 
 

Daily Inspiration