dongliming2416 2013-01-25 02:46
浏览 35

JavaScript进入Drupal模块

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.

  • 写回答

3条回答

  • drpmazn9021 2013-01-25 02:57
    关注

    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,
        );
    
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题