Reputation: 65
I'm trying to implement a JavaScript script into a Drupal 7 module and for some reason I keep getting this error here: "Parse error: syntax error, unexpected T_ECHO, expecting ')'"
I've been attempting this for a few hours now, but I'm just at a loss. Any help would be so greatly appreciated.
My code block indented to the far right. The other code is part of a module in Drupal 7 for your reference.
$node->content['actions'] = array(
'#theme' => 'links',
'#prefix' => '<div id="match-actions">',
'#suffix' => '</div>',
'#links' => _match_actions($node),
echo '<script type="text/javascript">'
, 'croll();'
, 'troll();'
, '</script>';
'#attributes' => array(
'class' => array('links', 'match-actions'),
),
'#heading' => t('Match actions'),
'#weight' => -10,
);
The JavaScript I'm trying to insert (as you can see my attempt in the echo above) is
function class_roll() {
// Car Classes
var classes = ["B", "A", "S", "R3", "R2", "R1"],
classToUse = classes[Math.floor(Math.random() * classes.length)];
document.getElementById("croll").innerHTML = classToUse ;
}
function track_roll() {
var tracks = ["Clear Springs Circuit", "Festival North Circuit", "Beaumont Circuit", "Finley Dam Circuit", "Gladstone Circuit", "Clifton Valley Trail", "Gladstone Trail", "Red Rock Descent", "Red Rock Hill Climb"],
trackToUse = tracks[Math.floor(Math.random() * tracks.length)];
document.getElementById("troll").innerHTML = trackToUse ;
}
What exactly am I doing wrong? I've been searching around Stack and all over the net which has enabled me to try different syntaxes, but I just can't get it to work. I'm not an expert in JS and PHP, but I'm trying to learn :). Again, any help is REALLY appreciated.
P.S. - In HTML terms, what I'm trying to do is this:
<p id="croll">Some text here</p>
<p id="troll">Some text here</p>
<button onclick="class_roll(); track_roll();">Class Roll</button>
but it would be great if instead of doing an onclick type of PHP action, it would be an onload type of event, but it would only onload for the first time and stick there and remain static.
Upvotes: 0
Views: 374
Reputation: 96
The better method, that won't break with things like Drupal AJAX, is to use Drupal behaviors.
(function ($) {
Drupal.behaviors.myModuleName = {
attach : function (context, settings) {
$(".match-actions", context).once('match-actions', function(){
croll();
troll();
})
}
}
})(jQuery);
Place that in a js file and load it with drupal_add_js(drupal_get_path('module', '{my module name}') . '{js file name}');
Upvotes: 0
Reputation: 1341
As HorusKol said you cannot directly call a JavaScript function inside your module. The reason being modules are written in PHP, and one cannot call functions of other programming language into one.
If you wish to insert JavaScript code, you should use the function drupal_add_js() to do so.
So, you could replace your echo
with the following:
echo drupal_add_js('croll();troll();','inline');
Upvotes: 0
Reputation: 8706
You can't put an echo inside of an array.
You should just be able to do:
$links = _match_actions($node);
$links[] = '<script type="text/javascript"> croll(); troll(); </script>'
$node->content['actions'] = array(
'#theme' => 'links',
'#prefix' => '<div id="match-actions">',
'#suffix' => '</div>',
'#links' => $links,
'#attributes' => array(
'class' => array('links', 'match-actions'),
),
'#heading' => t('Match actions'),
'#weight' => -10,
);
Upvotes: 2