"Execute asynchronous tasks in Javascript and receive the returned

Currently, I am utilizing JSF ajax within my application and unfortunately, we are unable to make any changes to that. In the process of waiting for user action, I find it necessary to prompt the user before executing the ajax method. Specifically, I need to inquire if the user wishes to revert all changes on the page using a custom dialog. If they choose to proceed with the reversion, I will invoke the Java method through ajax. However, if they decide against it, there is no need to execute the ajax.

To elaborate further:

Here is the code snippet for the revert button in my extensive page:

<h:commandButton value="#{msg.revertBtn}" accesskey="R" styleClass="actionButton" action="#{styleController.revertChanges}">

    <f:ajax execute="@this" onstart="return method();" render="styleCcFormtyleCcTable">

</h:commandButton>

Prior to initiating the ajax, it triggers my javascript function: method()

I require the user to remain engaged in method() until they click either the Yes or No button in my customized confirmation dialog, and then return true or false accordingly.

Do you have any suggestions on how to address this issue?

Answer №1

To prevent the default action of a button, you should attach an event listener to the button's `onclick` handler and return true or false based on whether you want to block the action. Attempting to block the action after the ajax request has already been executed will be too late.

One way to achieve this is by using JavaScript's built-in `confirm()` function as shown in the example below:

<h:commandButton ... onclick="return confirm('Are you sure?')">
    <f:ajax execute="@this" render="styleCcFormtyleCcTable" />
</h:commandButton>

Answer №2

It may not be the most conventional method, but I found a workaround that made it work for me.

Basically, I created a Custom Confirm Dialog with a variable to store the user's reply. When the user clicks on a button, their reply is stored in this variable.

Then, I added a click function to the button that checks the variable. If there is a reply, it proceeds with an action. If not, my custom dialog is called again.

Within the custom dialog, I simulate a button click with the saved reply. Just remember to clear the variable afterwards.

It took me two days to figure out this solution. Let me know if you want the code for it!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Parsley JS - Personalized Validation for Ensuring selected Items meet Minimum Value Requirements

Is it possible to validate a form so that at least 5 select boxes are set to Yes? If there are fewer than 5, the form should not submit and display an error message. I believe a custom validator is needed for this task. To see a complete example, check ou ...

When utilizing a wrapped v-text-field, it triggers warning messages and fails to update the data properly

Recently delving into the world of vue.js, I've been experimenting with creating a form using Vue, vuetify, and vuelidate. Oddly enough, when I don't wrap the v-text-field there are no issues, but as soon as I try to encapsulate it within compone ...

CSRF remains ineffective when set to true for INSERT, ADD, and DELETE operations

Looking to generate a CSRF token in the CodeIgniter framework, I have enabled csrf token changes to true. https://i.stack.imgur.com/7By0V.png $.ajaxSetup({ headers: { '<?php echo $this->security->get_csrf_token_name(); ?&g ...

What is the best way to manage Geo Location pop-ups in Mozilla and Google Chrome browsers when working with the Selenium WebDriver

Please take a look at the screenshot below: Is there a way to manage the Geo Location popup in Mozilla and Chrome browsers using Selenium WebDriver? package tiyotesting; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; ...

Node.js middleware for verifying required parameters

In my current application, I am utilizing node.js and express. I have developed multiple middleware functions, each one created in a similar fashion: function loadUser(req, res, next){ ... } I am interested in creating a middleware that can validate th ...

Struggling with adding two-digit numbers in a JavaScript calculator

While I can add single digits with no problem, adding double digits proves to be a bit tricky. The split method in this if statement is useful for isolating individual numbers, but it struggles when dealing with double digit numbers. if(e.value == '= ...

The axios requests are sent to the backend API, but the webpage remains empty without

I am trying to retrieve a base64 encoded image from my local backend by making a local API call. The logging on the backend confirms that axios is successfully calling the API, however, the frontend displays an empty page with no data. What could be caus ...

Unable to get @Query "encoded=false" to function properly in Retrofit 2

Utilizing Retrofit 2 to interact with the Bank API. Defined interface: public interface ApiService { @GET("statdirectory/exchange") Call<List<MyModel>> getСurrency(@Query("date") String inputDate); } Issue a ...

Removing all repetitions from an array in JavaScript

My collection of objects includes the following inputs: var jsonArray1 = [{id:'1',name:'John'},{id:'2',name:'Smith'},{id:'3',name:'Adam'},{id:'1',name:'John'}] There is a dupl ...

How to Implement Multiple OR Statements in SharePoint 2010 Using Jquery and JSON Rest

Struggling to create a multiple OR variable, but it just won't work. Is there an issue with my syntax or logic? var category = (i.Category == "Electric" ? "E" : i.Category == "Power Module" ? "PM" : i.Category == "Branch Office" ? "BO" : ""); After ...

JavaScript finding items in an array

The main issue I am encountering involves receiving a notification when the term (city name within the project) entered by the user in JqueryUI Autocomplete does not match anything in the collection (e.g. user entered "My Sweet City" and it does not matc ...

Guide on setting the href value within a $.each loop in AJAX requests

I am struggling with downloading a file within a thread. The issue I'm facing is that I can't set the file name in the href and download attributes, so when I attempt to download it, it shows no file available. However, I did realize that replaci ...

What could be causing my ajax request to not be successfully compiled?

I seem to be experiencing an issue with this part of my code. Despite trying multiple methods, it still isn't functioning correctly. What steps can I take to resolve this problem? $.ajax({ url: "https://corona-api.com/countries/BR", ...

The most efficient method for documenting $.trigger in JavaScript/jQuery is through the use of JSD

When it comes to documenting in jsDuck, what is the optimal method for capturing the following event trigger: $(document).trigger('myCustomEvent'); ...

Retrieving precise information from the backend database by simply clicking a button

As a new full stack programmer, I find myself in a challenging situation. The root of my problem lies in the backend table where data is stored and retrieved in JSON format as an array of objects. My task is to display specific data on my HTML page when a ...

The functionalities of Google Maps are experiencing issues within AngularJS when utilizing the $route feature

Having Issues with Google Maps in AngularJS when using <ng-view></ng-view> Routing Configuration app.config(function($routeProvider, $locationProvider) { $locationProvider.html5Mode(true); $routeProvider .when('/', { t ...

Posting an image may be successful on certain devices, but may result in the error "Stream FixedLengthSource .inputStream()

I'm currently working on an Android app that allows users to upload images to a specific directory using PHP and okhttp. The frontend and backend are functioning correctly on my Samsung device, but they crash on non-Samsung devices. Upon investigatin ...

Full-screen modal fade not functioning properly

I am trying to implement multiple fullscreen modals on a single page, but I am facing an issue where they slide in and out at an angle instead of just fading in and out. I have been unable to achieve the desired effect of a simple fade transition. If you ...

Embracing PWAs with subdomains – seamless installation

One of my Progressive Web Applications (PWA) called app A contains a link to another app, app B. Initially, I hosted both apps on the same subdomain (for example: ) and everything worked perfectly - installing app A also installed app B. However, when I a ...

Backend is currently unable to process the request

Whenever a user clicks on a note in my notes page, a request is supposed to be made to the backend to check if the user is the owner of that particular note. However, for some reason, the request is not being processed at all. The frontend is built using ...