0

I had some trouble with logout button, so i use confirm(); with on click event on jQuery. I use if statement for confirm that the user is click Ok or Cancle

Button script :

<a id="logout-btn">
    <p>Logout</p>
</a>

Script that working :

$("#logout-btn").on("click", function() {
    var acc = confirm("Are you sure want Logout?");
    if (acc == true) {
        window.location.href = "logout.php";
    }else{

    }
});

Script that isn't working :

$("#logout-btn").on("click", function() {
    var acc = confirm("Are you sure want Logout?");
    if (acc == true) {
        window.location.href = "../logout.php";
    } else {

    }
});

The different between script just different folder but the second script when i click Cancel it still logout, What is the problem??

  • 1
    That is not possible from the code you show. Where is the button? In a form? Or is there some dialog template running too? – mplungjan Apr 20 '20 at 07:43
  • Please create a [small demo](https://stackoverflow.com/help/minimal-reproducible-example) for this using [jsfiddle](https://jsfiddle.net/) or [snippet](https://meta.stackoverflow.com/a/358993/1823841) here to show the issue happening. – palaѕн Apr 20 '20 at 07:44
  • but the first script is working – Fragmantedbin Apr 20 '20 at 07:44
  • The two scripts are identical. There is no jQuery issue here. Just because the URL is different. Look in the network tab for what is actually happening - like a 404 that redirects to home – mplungjan Apr 20 '20 at 07:45
  • when i on other folder i have to use the second script, but why it doesnt working just because diferent url – Fragmantedbin Apr 20 '20 at 07:48
  • 1
    Also if you use anchors, then `$("#logout-btn").on("click", function(e) { e.preventDefault();` – mplungjan Apr 20 '20 at 07:52
  • 1
    ow thanks that work for using preventDefault – Fragmantedbin Apr 20 '20 at 07:57

0 Answers0