简体   繁体   中英

load the js in successful login after redirect

I want to load the js file after redirect to destination in drupal. 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.

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. 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:

Firing the JS using another JS function

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. We will use this later in our destination page to run the JS function that we need.

Example jQuery code you need to place in the destination page. For this to work you need to have the function one_time_popup() already loaded in your code. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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