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

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败