Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsf can I close browser tab from bean?

Tags:

java

jsf

My application is managing software, and for user convenience, I want to allow them to open multiple tabs for changing parameters of more than one record at a time. But after finishing whatever they doing, the tabs stays open, and I got some complains about that. So basically my question is:

If there's any way to close browser tab that sends a request to method in my backing bean? for example:

JSF page:

<h:commandButton value="Public score"
   action="#{assignmentBean.publicSelected()}">                        
</h:commandButton>

Bean method:

public void publicSelected() {
    application.setAssignmentStatus(done);
    dataAccess.mergeEntity(application);
}

is there any way to add something after merging command and close browser tab that activated method? Thanks for help

FULL CODE FOR SOLUTION I'm bad with mixing JS and JSF, so for any of you that are also bad at this I post full code solution using Tiago Vieira Dos Santos hint.

Now my button code looks like:

<h:commandButton value="Public score"
   action="#{myBean.doThings}">    
   <f:ajax execute="@this" onevent="pop"/>                    
</h:commandButton>

plus on bottom of page I added code as follows:

<script type="text/javascript">
    function pop(data){
        if(data.status == "success"){
            window.close();
        }
    }
</script>

now after method does what has to be done the window closes.

like image 865
Lukas Novicky Avatar asked Apr 08 '14 13:04

Lukas Novicky


People also ask

How do I close the current browser tab?

Open Chrome browser. Choose an option: On Windows & Linux, at the top right, click Close.

Can you close browser with JavaScript?

As of 2017, there is no way to close a web browser tab or window using JavaScript. Modern security standards do not permit this behavior.

What is the use of closing tab?

close tab because you indicate the last page and it closes that. close application is when you use a desktop application.


2 Answers

I think you can be use the javascript command Window.close() then you can put it on oncomplete tag or call in you managed bean using the FacesContext.

See more in this How to close current tab in a browser window?

like image 100
Tiago Vieira Dos Santos Avatar answered Oct 30 '22 08:10

Tiago Vieira Dos Santos


Using an OutputLink and Javascript

<h:outputLink onclick="window.open('popup.faces', 'popupWindowName', 'dependent=yes, menubar=no, toolbar=no'); return false;" value="#">
        <h:outputText value="open popup" />
</h:outputLink>

With this solution we got control over the appearance of the new browser window. And since there is no postback, there is no validation at all. This is the easiest way to open a new browser window when no model update is needed and no action has to be executed.

In order to implement a proper action handling we need to move the decision whether to open a new window to the action listener.

<h:commandLink actionListener="#{bean.openPopupClicked}" value="open popup" />

public void openPopupClicked(ActionEvent event) {
    // code to open a new browser window goes here
}
like image 39
Benjamin RD Avatar answered Oct 30 '22 09:10

Benjamin RD