You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

#1 Javascript Approach

User clicks the joget generic logout button OR

We'll be using a script that will:

  • target the logout <a> anchor
  • interrupt any action on it by using event.preventDefault()
  • execute the logout API inside the function


Insert the provided script into the Custom Javascript field in the UI Builder advanced settings:


Script:

$(document).ready(function(){
  logoutBtn = $('a[href="/jw/j_spring_security_logout"');

  $.each(logoutBtn, (k,v)=>{
    console.log($(v));
    $(v).on('click', function(e){
       e.preventDefault();
       //Logout current app
       window.location.href = "http://localhost:8080/jw/j_spring_security_logout";
       //API Logout
       window.location.href = "https://{hostname}/api/login/logout";
    })
   })
})


Additionally,

If you do not want to receive the following alert:

Use this script instead

window.addEventListener("beforeunload",function (event){
  event.stopImmediatePropagation();
})
$(document).ready(function(){
  logoutBtn = $('a[href="/jw/j_spring_security_logout"');

  $.each(logoutBtn, (k,v)=>{
    console.log($(v));
    $(v).on('click', function(e){
       e.preventDefault();
       //Logout current app
       window.location.href = "http://localhost:8080/jw/j_spring_security_logout";
       //API Logout
       window.location.href = "https://{hostname}/api/login/logout";
    })
   })
})

The script will inform the browser not to execute any other "beforeunload" event handlers once this one is triggered.


Keep in mind that using stopImmediatePropagation in this context can have side effects. If there are other tasks or clean-up operations you need to perform when the page is unloaded, they will be skipped. It's important to consider the broader context of your application and whether this behavior aligns with your goals.


Reference:

https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event 

https://stackoverflow.com/questions/72224869/how-to-disable-leave-site-changes-you-made-may-not-be-saved-pop-up-in-angula


  • No labels