File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Using JavaScript for a conditional action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Using JavaScript for a conditional action" Watch "Using JavaScript for a conditional action" New topic

Using JavaScript for a conditional action

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
Hello Ranchers,

I'm sure this is a very simple question, but I can't find an obvious solution.

I have a command button that on clicking "Clear List" clears a list of uploaded filenames displayed on the screen, but at the moment it does not delete the files in the folder that have been uploaded. I can make the changes for this, but I want to warn the user that his files will be deleted from the server when he clicks the button, and give him the option of aborting the process.

The xhtml code is here:

and on clicking "Clear List" a dialog box appears asking him if he is sure. The idea is that if he clicks "OK", the files are deleted and the display is reset, otherwise nothing happens. However, the display is always reset regardless of the reply, and thus the files will be deleted once I have written the code for that.

Without changing any of the Java code in the bean, how do I arrange for the button only to work when onclick returns true? I'm usinmg JFS 2.

There must be an easy way to do this and would most apprecaite some advice.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155

I made a few changes on got this to work, but there is a "page flip".

The code in the xhtml file is now:

The only change was to replace onclick="confirm('Are you sure?')" by onclick="uploadClear()" in the command button, with uploadClear() being in a separate js file as follows:

In my previous post I had not included the data table, which is displayed on the screen. This is part of a form containing much more information.

Before the JavaScript was added, this worked correctly when clicking the "Clear List" button, and the display was cleared without a "page flip". With the JavaScript added, when the user clicks "OK" in the popup box, the display is again cleared without a "page flip", as it should be. However, when the user clicks "Cancel" in the popup box, the page does flip, but the displayed contents in the data table are not cleared, which is what I want. However, why do I get an irritating "page flip" when the "Cancel" is selected and the display stays the same, but not when "OK" is selected and the list is cleared?

I noticed that I had to invert the logic in the JavaScript function to get it to work the correct way round. Does anybody have any suggestions how to fix this, I would be most grateful to hear?

The idea is eventually to get files in an uploaded folder cleared, but it would be nice to get the display fixed first.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

uploadClear() returned results, but you didn't return them in their turn to the commandButton.

This works exactly the same way as it does for a bog-standard HTML SUBMIT button, incidentally.

An IDE is no substitute for an Intelligent Developer.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
OK, many thanks, being so tied up in the details of JSF 2, it's easy to forget bog standard HTML.

I put the whole JavaScript in the command button as follows:

and it now works correctly. "return confirm(...)" returns true or false, as needed and there is no more "page flip". Moreover, the logic is the correct way round.

Many thanks
I agree. Here's the link:
subject: Using JavaScript for a conditional action
It's not a secret anymore!