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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?