Javascript 在特定页面中加载JS

Javascript 在特定页面中加载JS,javascript,php,drupal,Javascript,Php,Drupal,我将其用于具有URL的特定页面,例如/register、/create等。。。代码是: function mytheme_preprocess_page(&$variables) { // page path if (arg(0) == 'fbconnect' && arg(1) == 'register' && arg(2) == 'create') { drupal_add_js(drupal_get_path('

我将其用于具有URL的特定页面,例如/register、/create等。。。代码是:

function mytheme_preprocess_page(&$variables) {
     // page path
      if (arg(0) == 'fbconnect' && arg(1) == 'register' && arg(2) == 'create') {
        drupal_add_js(drupal_get_path('theme', 'mytheme') . '/js/scrollTo.js', 'file');
      }
    }
所以现在我在想,我该如何为CSS类加载JS,而不是页面URL,并意识到为arg添加类不起作用,所以下面的方法不起作用:

  if (arg() == 'form-item-search-block-form') {
    drupal_add_js(drupal_get_path('theme', 'mytheme') . '/js/jquery.placeholder.js', 'file');
  }

有什么想法吗?这里是新手。

您的第一个示例仅与“fbconnect/register/create”url匹配。您可以通过以下方式更轻松地执行此操作:

if ('fbconnect/register/create' == current_path()) {
对于第二种情况,您需要通过定制模块中特定于搜索块表单的formalterhook来实现。假设表单id是
search\u block
,它应该是:

function mymodule_form_search_block_alter(&$form, &$form_state, $form_id) {
    drupal_add_js(drupal_get_path('theme', 'mytheme') . '/js/jquery.placeholder.js', 'file');
}