开发者

jQuery: Modal window onload with fancybox + cookies

开发者 https://www.devze.com 2023-02-15 21:14 出处:网络
I\'ve got a script I\'m using that loads the window, sets t开发者_JS百科he cookie but and the window doesn\'t appear again if the user clicks \"No Thanks\" but I want the window not to appear if they

I've got a script I'm using that loads the window, sets t开发者_JS百科he cookie but and the window doesn't appear again if the user clicks "No Thanks" but I want the window not to appear if they also click "Take the Survey". I tried adding the second ID here $('#modal_close, #modal_exit').live('click', function(e) { but it doesn't work. Instead of going to the survey link, it goes to the page the modal is firing from. Any clues and/or help?

$(document).ready(function(){

// MODAL BOX if the requested cookie does not have the value I am looking for show the modal box

if($.cookie("cookieName") != 'true')
{
    var _message_to_show = 'Window Content Goes Here';

    // on page load show the modal box
    // more info about the options you can find on the fancybox site
    $.fancybox(
        _message_to_show,
        {
            'width'             : 550,
            'height'            : 275,
            'transitionIn'      : 'fade',
            'transitionOut'     : 'fade',
            'centerOnScroll'    : 'true',
            'overlayOpacity'    : 0.8,
            'overlayColor'      : '#000',
            'modal'             : 'true',
    'padding'       : 5
        }
    );

    // in the message is a link with the id "modal_close"
    // when you click on that link the modal will close and the cookie is set to "true"
    // path "/" means it's active for the entire root site.. if you set it to "/admin" will be active on the "admin" folder
    // expires in 7 days
    // "modal" is the name i gave the cookie, you can name it anything you want
    $('#modal_close').live('click', function(e) {
        e.preventDefault();
        $.cookie("cookieName", "true", { path: '/', expires: 7 });    
        $.fancybox.close()
    });
}    

});


So I figured it out right after posting here... Isn't that always the way? Anyhoo, for someone that may be looking to do the same thing I added the following:

$('#modal_exit').live('click', function(e) {
    $.cookie("cookieName", "true", { path: '/', expires: 7 });    
    $.fancybox.close()
});

I removed e.preventDefault(); because clicked anchors will not take the browser to a new URL if that is triggered. Removing it allowed the cookie to be checked and for my link to take me where I wanted to go.

0

精彩评论

暂无评论...
验证码 换一张
取 消