function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EPSWDEVEPSWDEV 

OnUnload Event for a visual force page

I am trying to capture the 'onunload' javascript event on a visual force page. Adding a html body tag and then adding the onunload event dosnt work. How can I capture this event.

In general I am trying to prompt the user when they move away from this page (either to another tab or link within the browser or if they decide to close the browser).

Thanks
Best Answer chosen by Admin (Salesforce Developers) 
RajanJasujaRajanJasuja

Hi Gays,

 

I know I am late. But just to close this discussion.

I Changed  

 

window.onbeforeunload = (on) — unloadMessage : null;
 window.onbeforeunload = (on) ? unloadMessage : null;

 

 

and it’s working for me.

 

Cheers!

Rajan Jasuja

All Answers

JimRaeJimRae
Did you try something like this?  By using onbeforeunload, you have the option, if the user cancels, to return them to the page.

Code:
<script LANGUAGE="JavaScript1.2" TYPE="text/javascript">
    function unloadMessage(){
        message = "Wait! You haven't finished."
        return message;
    }
    function setBunload(on){
        window.onbeforeunload = (on) — unloadMessage : null;
    }
    setBunload(true);
</script>

 

EPSWDEVEPSWDEV
Thanks for you response.

I used your code by embedding it after the <apex:page> tag and unfortunately I can still move away to other pages without displaying the alert.

Also I see you dont use the alert function -- ?? how is the message displayed that case. Does setting a value to window.onbeforeunload  mean that 1)  the property is now 'active' aka will be triggered 2) display the expression result specfiied in the property in a alert window ?

Thanks
JimRaeJimRae
That is odd, I tried it both in the page area, and inside of a form tag and it worked in both locations, on both IE7 and FF3.0.1.
It provides a notice, similar to an alert when I go to another page (or tab in SDFC) or when I try to close the browser from the window control.
The message is passed into the window event and then get's displayed, you don't need to use an alert box.
Depending on what you are trying to do, this could have issues, and should have some more advanced features before implementing in production.  For example, you might want to have another variable that gets tested to see if the function should fire or not, if the user has saved the information (for example) you set the "saveddata" variable to true, and don't generate the message.  The other condition that can be annoying is that a refresh of the page, or even the section the code is in will cause the onbeforeunload event to fire.

Good Luck!
EPSWDEVEPSWDEV
Thanks. I try to see what I do, in the mean time could you please post the code for  visual force page you created/tested ?


JimRaeJimRae
Here you go:

PAGE:
Code:
<apex:page controller="wrap2con">
<script LANGUAGE="JavaScript1.2" TYPE="text/javascript">
    function unloadMessage(){
        message = "Wait! You haven't finished."
        return message;
    }
    function setBunload(on){
        window.onbeforeunload = (on) — unloadMessage : null;
    }
    setBunload(true);
</script> 
  <apex:form >
  <apex:pageBlock >
  <apex:pageblockButtons >
      <apex:commandButton action="{!doSearch}" value="Search" rerender="output"></apex:commandButton>
  </apex:pageblockButtons>
  <apex:inputText value="{!SearchSolutionText}"></apex:inputText>
  </apex:pageBlock>
  <apex:outputPanel >
  <apex:pageBlock >
  <apex:pageblockTable value="{!results}" var="v">
    <apex:column value="{!v.SolutionName}"/>
    <apex:column value="{!v.SolutionNote}"/>
    <apex:column value="{!v.CreatedDate}"/>
  </apex:pageblockTable>
  </apex:pageBlock>
  </apex:outputPanel>
  </apex:form>
</apex:page>

 
CONTROLLER:
Code:
public class wrap2con {
    String searchSolutionText;
    List<List<sObject>> tresults;
    List<Solution> results;
    
    public String getSearchSolutionText() {
        return searchSolutionText;
    }
    
    public void setSearchSolutionText(String s) {
        searchSolutionText = s;
    }
    
    public List<Solution> getResults() {
        return results;
    }

    public PageReference doSearch() {
        tresults = [FIND :searchSolutionText RETURNING Solution(SolutionName, SolutionNote, CreatedDate)];
        results = (List<Solution>)tresults[0];
    return null;
    }
}

 

EPSWDEVEPSWDEV
After looking at your code I have too agree this is quite odd. I am using the script in a similar fashion - A VF page with a custom controller implementing some actions. however the darn thing just navigates away when I click on another link ..........Anyways appreciate your help.

Thanks


Message Edited by EPSWDEV on 01-12-2009 11:44 AM
RajanJasujaRajanJasuja

Hi Gays,

 

I know I am late. But just to close this discussion.

I Changed  

 

window.onbeforeunload = (on) — unloadMessage : null;
 window.onbeforeunload = (on) ? unloadMessage : null;

 

 

and it’s working for me.

 

Cheers!

Rajan Jasuja

This was selected as the best answer
trick.ax1374trick.ax1374

Hi Rajan,Jim and other

 

I know this topic has been addressed long bank.However,I need pretty much the same stuff.Rajan,your suggestion really works and it is helpful

My question is.How do I override the message which I see in the message box.

Message is:-This page is asking you to confirm that you want to leave - data you have entered may not be saved.

How do I override the above message,Atleast,I need to get rid of the red lines ?

Is there some other way of showing dialog box with the option stay or leave on the current page?

I have visual force tab and person clicks on the tab and does some wotk in the application  underneath the tab and then saves some report.

 

He is not entering any data in there.

I want to get rid of  the lines in red

Can somebody throw some light on it.?

 

Code that I am trying is pretty much what you see above?

 

 

 

Thanks,

Trick0123

 

 

 

trick.ax1374trick.ax1374

Hi Rajan,Jim and other

 

I know this topic has been addressed long bank.However,I need pretty much the same stuff.Rajan,your suggestion really works and it is helpful

My question is.How do I override the message which I see in the message box.

Message is:-This page is asking you to confirm that you want to leave - data you have entered may not be saved.

How do I override the above message,Atleast,I need to get rid of the red lines ?

Is there some other way of showing dialog box with the option stay or leave on the current page?

I have visual force tab and person clicks on the tab and does some wotk in the application  underneath the tab and then saves some report.

 

He is not entering any data in there.

I want to get rid of  the lines in red

Can somebody throw some light on it.?

 

Code that I am trying is pretty much what you see above?

 

 

 

Thanks,

Trick0123

 

 

 

XenoPuppyXenoPuppy

Hello, I tried out this solution on a custom visual force page used for editing purposes. The problem I am running into is the user is the pop-up will appear whenever the user natvigates away from the page, like during a "save" or "cancel" operation. However I only want the warning to pop-up when the user closes the tab. 

 

Also, the pop-up will appear in other places such as entering a value for a lookup field and clicking on "today"'s date on a date field.

 

For the first part of the problem, I think it might be possible to modify the controller and use a boolean to stop the pop-up (however I haven't explored it yet).

 

But for the second part, it seems like something that is out of my control (where the pop-up is appearing for date fields and lookup fields).

 

I would appreciate any ideas/inputs, thanks a lot!