简体   繁体   English

重定向后加载js以成功登录

[英]load the js in successful login after redirect

I want to load the js file after redirect to destination in drupal. 我想在重定向到drupal中的目标后加载js文件。 I have created a custom module with a hook_user_login.I have redirect a page in successful login and want to load a js file after redirect.now file loads in between login success and until redirect. 我已经创建了一个带有hook_user_login的自定义模块。我已经在成功登录后重定向了页面,并希望在重定向后加载js文件。现在文件将在登录成功到重定向之间加载。

function one_time_popup_user_login(&$edit, $account){
$userName='test'; 
if(!isset($_COOKIE[$userName])){
$count=1;
  if($count==1)
     {
          drupal_add_js(array('one_time_popup' => array('aniv' => $anniversaryCount,'userName'=>$userName,'celeType'=>'Anniversary')), array('type' => 'setting'));
drupal_add_js(drupal_get_path('module', 'one_time_popup') . '/celebrationPopup.js','file');
       $settings=variable_get('one_time_popup_effects',unserialize(ONE_TIME_POPUP_DEFAULT)); 
       drupal_add_js(array('onetimepopupmenu'=>$settings),'settings'); 
       setcookie($userName, '1', time()+(24 *3600));
     }
if (!isset($_GET['destination'])) {
        $_GET['destination'] = drupal_get_destination(); //get the current url 
     }
}
}

I think you need to have the JS in your destination page. 我认为您需要在目标页面中包含JS。 And tell that page when to run it. 并告诉该页面何时运行。 Or add it in a hook that targets that particular page. 或将其添加到针对该特定页面的挂钩中。

First I would include some extra validation in your hook so the redirect doesn't happen when a user is trying to recover the password. 首先,我将在钩子中添加一些额外的验证,以便在用户尝试恢复密码时不会发生重定向。

See how I am checking for the 'user_pass_reset' form id in the following exampes: 在以下示例中查看如何检查“ user_pass_reset”表单ID:

Firing the JS using another JS function 使用另一个JS函数触发JS

function one_time_popup_user_login(&$edit, $account) {
      if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset') {
        $_GET['destination'] = 'your/custom/path#celebrationpopup';
  }
}

Notice how I've added a hash (#celebrationpopup) to the destination url. 请注意,我是如何将哈希(#celebrationpopup)添加到目标网址的。 We will use this later in our destination page to run the JS function that we need. 我们稍后将在目标页面中使用它来运行所需的JS函数。

Example jQuery code you need to place in the destination page. 您需要在目标页面中放置示例jQuery代码。 For this to work you need to have the function one_time_popup() already loaded in your code. 为此,您需要在代码中加载函数one_time_popup()。 This is just an example. 这只是一个例子。

$(document).ready(function() {
  //Get hash from URL
  var hash = location.hash;

  if (hash == '#celebrationpopup') {
    one_time_popup();
  }
});

Another option: Using a hook 另一个选择:使用钩子

If your destination page is a node you can use: 如果目标页面是节点,则可以使用:

function one_time_popup_user_login(&$edit, $account) {
  if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset') {
    $_GET['destination'] = 'your/custom/path?celebrationpopup=true';
  }
}


function one_time_popup_node_view($node, $viewmode, $langcode) {
  if($node->type == 'some_type' && isset ($_GET["celebrationpopup"])) // You can also use a node ID.
  {
    drupal_add_js(array('one_time_popup' => array('aniv' => $anniversaryCount,'userName'=>$userName,'celeType'=>'Anniversary')), array('type' => 'setting'));
    $node->content['#attached']['js'][] = array
    (
      'type' => 'file',
      'data' => drupal_get_path('module', 'one_time_popup') . 'js/celebrationPopup.js',
    );
  }
}

You might as well use a page preprocess function. 您也可以使用页面预处理功能。

Hope this is clear. 希望这很清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM