0

the situation was like this. I have a bootstrap coded modal. When I click a submit button, it gets triggered and pop-out a box that has a yes or no plus input box in it. So either I click yes or no, it will just close the box. Some other pages also has this popup up. the problem is, after the modal closed down, when I clicked the back button, of the page, the modal also pops out when there's no javascript that triggers to fire it. the previous code of the back button was like

go.history(-1)

then I tried to changed it to

windows.history.back()

it didn't helped at all, it still pops out the modal . any ideas how to fix this issue?

sasori
  • 5,249
  • 16
  • 86
  • 138
  • I think the issue here is in the code that open the popup as it should prevent the default behaviour: reloading the page. You should call the *event.preventDefault()*, with *event* as the first parameter of the handler function. – Mario Santini Aug 19 '16 at 14:25
  • but it's a bootstrap. I never and don't wish to modify the core javascript of bootstrap. all I know is, the data-target thing in the html code of the button is the one triggering the div id of the modal popup – sasori Aug 19 '16 at 14:28
  • please share some more code, like where you open the dialog. – Mario Santini Aug 19 '16 at 14:32

1 Answers1

0

You may set a cookie. When it exists, simply dont show your modal.

Here is an example:

Note: I will use a snippet I found here.

var cookieName = 'home_modal';
if(!readCookie(cookieName)){
    showModal();

    //the modal will not show up for 1 day
    createCookie(cookieName, 1, 1);
}

// #### [Base functions] ####

// Cookies
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";               

    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(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 eraseCookie(name) {
    createCookie(name, "", -1);
}

function showModal() {
    //your modal logic goes here
}
Community
  • 1
  • 1
CarlosCarucce
  • 3,420
  • 1
  • 28
  • 51
  • any othe solutions or ideas ? – sasori Aug 19 '16 at 14:29
  • @sasori not ATM for pure javascript. But if your script is based on php, you can also set a [php session](http://php.net/manual/pt_BR/reserved.variables.session.php) and create a javascript flag based on it and, again, if this flag value is set, dont call your _showModal()_ function – CarlosCarucce Aug 19 '16 at 14:33
  • but unfortunately, the thing is in bootstrap, I'm not very good in html nor javascript nor css – sasori Aug 19 '16 at 14:34
  • Can you show us a piece of your "display modal" code? Or may be put a link?? – CarlosCarucce Aug 19 '16 at 14:38