using javascript

Events happening in the community are now at Drupal community events on www.drupal.org.
royerd's picture

Maybe I should have titled this "stealing javascript."

This is surely too big of a question for this forum, but say I wanted to mimic the navigation banner on the gvsu.edu webpage--www.gvsu.edu (the logo, news and events, quick links, etc) that is across the top). Note that it uses javascript to create the drop down etc.

I know how to pull in that php code into a page.tpl.php file and I know how to use CSS, but I know nothing about the java on that page. How do I use that script (I am actually trying to replicate that page more or less on a drupal site). Can anyone point me in the right direction?

Dan Royer

Comments

You might find a module to

pfahlr's picture

You might find a module to handle your project requirements in here:
http://drupal.org/project/modules?filters=tid:124&solrsort=sis_project_r...

Otherwise...

You'll need to create a module (if one doesn't exist already).

The "Drupal" way of handling javascript goes something like this:

1)add a javascript file with defined behaviors

<?php
...
mymodule_init(){
//add main javascript
drupal_add_js(drupal_get_path('module', 'module name').'/your_script.js', 'module');
...

2)add any variables for the script in php

<?php
function mymodule_somehook()
{
$aData = array();
$aData['mymodule'] = array('var'=>'value', 'var2'=>'value2' ... [etc]
drupal_add_js($aData, 'setting');

This array will now be available in the global javascript object Drupal.settings.

<script type="text/javascript">
alert(Drupal.settings.mymodule.var); //prints "value"
alert(Drupal.settings.mymodule.var2); //prints "value2"
</script>

This is worthwhile, you'll never find yourself writing

<?php
$sHTML = '
<script type="text/javascript>
aVal = '.$somephpval.';
...
</script>
';

print $sHTML;

or any other such inelegant code in templates further decoupling the theme from functionality.

Here's some articles that should prove useful:
http://api.drupal.org/api/function/drupal_add_js/6
http://api.drupal.org/api/function/drupal_get_path/6
http://www.slideshare.net/tomfriedhof/drupalbehaviors
http://www.nicklewis.org/drupal-hackers-cookbook/jquery-js/unlocking-dru...

Grand Rapids, MI

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: