我正在尝试在我的自定义模块中使用 jquery ui 实现来构建简单的模式窗口。我想在用户访问我的网站时显示一次此模式。我选择使用 cookie 实现。当用户访问我的网站时,如何使此模式向用户显示一次?我应该使用 hook_init 吗?
这是我的代码:
mymodule.module
/* menu callback */
function mymodule_menu() {
$items['mymodule-popup'] = array(
'title' => 'Join Club',
'page callback' => 'mymodule_ajax_register',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
function mymodule_ajax_register() {
$path = drupal_get_path('module', 'mymodule');
drupal_add_library('system', 'ui.dialog', false);
drupal_add_library('system', 'ui.draggable', false);
drupal_add_js($path.'/mymodule.js');
$output = '';
$webform_nid = 30;
$node = node_load($webform_nid);
$submission = (object) array();
$webform = drupal_get_form('webform_client_form_'.$webform_nid, $node, $submission);
$output .= '<div id="popup">';
$output .= drupal_render($webform);
$output .= '</div>';
return $output;
}
mymodule.js
(function ($) {
$(document).ready(function(){
$('#popup').dialog({
height: 'auto',
width: 700,
autoOpen: false,
modal: true,
resizable: false
});
$('a').click(function(){
var status = false;
if(this.className !== 'lightbox-processed'){
if(!getCookie('newspopup')){
setCookie('newspopup', 'true', 1);
$('#popup').dialog('open');
}else{status = true;}
if(this.className === 'ui-dialog-titlebar-close ui-corner-all ui-state-hover'){
$('#popup').dialog('close');
status = true;
}else{
}
}
if(this.id === 'bottomNavClose'){
$('#popup').dialog('close');
}
return status;
});
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)===' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function deleteCookie(name) {
setCookie(name,"",-1);
}
});
})(jQuery);
使用这些代码,当用户访问 myweb.com/mymodule-popup 然后单击任何链接/网址时,将出现模式窗口(弹出窗口)。
每次用户访问我的网站时,如何使模态窗口出现一次?
我需要你们的建议,谢谢。