用户访问网站时打开模态窗口

时间:2013-09-04 07:09:34

标签: jquery jquery-ui drupal-7 drupal-modules

我正在尝试使用自定义模块中的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然后点击任何链接/网址时,将出现模态窗口(弹出窗口)。

每次用户访问我的网站时,如何制作模态窗口?

我需要你们的建议,谢谢你们。

1 个答案:

答案 0 :(得分:0)

如果您想保存用户的cookie,请使用user_cookie_save函数(在php中)并通过js代码中的 Drupal.visitor.key 变量将其恢复。无需在代码中设置cookie,只需从Drupal中检索它。

另请参阅set a jQuery cookie to show popup only once

相关问题