Using Bootstrap, have alert box remember close action

蹲街弑〆低调 提交于 2019-12-04 01:28:47

Code issues

  • In your codes onload function you seem to be setting the cookie value which is strange since you only want to set the cookie when the user has closed the alert box.

  • Your button has a href attribute. This is not necessary as well as invalid html.

Solution

In order to simply hide and remember the state of the alert box you have to bind an event to the close button so that you know when the user has clicked close.

To bind an event using jQuery you can use the following code:

// When document is ready replaces the need for onload
jQuery(function( $ ){

    // Grab your button (based on your posted html)
    $('.close').click(function( e ){

        // Do not perform default action when button is clicked
        e.preventDefault();

        /* If you just want the cookie for a session don't provide an expires
         Set the path as root, so the cookie will be valid across the whole site */
        $.cookie('alert-box', 'closed', { path: '/' });

    });

});

To hide the alert box you should simply check for the correct cookie value and hide the box:

jQuery(function( $ ){

    // Check if alert has been closed
    if( $.cookie('alert-box') === 'closed' ){

        $('.alert').hide();

    }

    // ... Binding code from above example

});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!