我想在重定向到drupal中的目标之后加载js文件。我已经创建了一个带有hook_user_login.I自定义模块,该模块在成功登录时重定向页面,并希望在登录成功和重定向之间加载redirect.now文件后加载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
}
}
}发布于 2017-09-08 08:11:28
我认为你需要在你的目标页面中有JS。并告诉该页面何时运行它。或者将其添加到针对该特定页面的钩子中。
首先,我会在你的钩子中加入一些额外的验证,这样当用户试图恢复密码时,重定向就不会发生。
在下面的示例中查看我是如何检查'user_pass_reset‘表单id的:
使用另一个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';
}
}请注意,我是如何向目标url添加散列(#celebrationpopup)的。稍后我们将在目标页面中使用它来运行我们需要的JS函数。
需要放在目标页面中的示例jQuery代码。为此,您需要将函数one_time_popup()加载到您的代码中。这只是一个例子。
$(document).ready(function() {
//Get hash from URL
var hash = location.hash;
if (hash == '#celebrationpopup') {
one_time_popup();
}
});另一种选择:使用钩子
如果您的目标页面是一个节点,您可以使用:
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',
);
}
}您也可以使用页面预处理函数。
希望这一点是清楚的。
https://stackoverflow.com/questions/45978333
复制相似问题