19.4 Generating JavaScript Rollovers


You want to have PHP generate your JavaScript for you.

Technique

Use PEAR's Javascript_Rollover class:

 <?php $links = array('http://www.internet.com/',               'http://www.designmultimedia.com/',               'http://www.redhat.com/'); $rollovers = new Javascript_Rollover($links); $rollovers->types(ROLLOVER_MOUSEOVERROLLOVER_MOUSEOUT); $rollovers->prefix('somepage'); $rollovers->ending('jpg'); ?> <html> <head>     <title> Somepage </title>     <script language="javascript">     <?php         print $rollovers->genPreloadData();     ?>     </script> </head> <body> <!-- Place the images in a table --> <table border='0' cellpadding='0' cellspacing='3'> <tr> <td> <?php foreach ($rollovers->genRollovers() as $rollover) {     print "$rollover\n"; } ?> </td> <td> Page contents here </td> </tr> </table> </body> </html> 

Comments

As I said in the second sentence of this book, PHP is a language that helps you do more. PHP is excellent at generating JavaScript code, saving you the time and the effort of writing all that horrendous HTML and JavaScript. Because a Web page is parsed by PHP and then sent to the browser where the HTML and JavaScript are manipulated, you can easily generate buttons by using PEAR's Javascript_Rollover class.

The Javascript_Rollover class has two interfaces, the advanced interface shown earlier, which generates rollovers that preload your images (where your images are named prefix imgNum .ending ), and rollovers that will generate an embedded rollover when given an array:

 <?php $rollovers = new Javascript_Rollover; print $rollovers->genRollover(array('link' => 'http://www.internet.com',                   'mouseOver' => 'image1_on.gif','mouseOut'  =>                   'image1_off.gif')); ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net