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
AmphitriteAmphitrite 

VF javascript to run actionfunction

I have some Javascript that should trigger an actionFunction method if user tries to close window or browser. However - it isn't actually running. Any ideas?

 

 

<script LANGUAGE="JavaScript1.2" TYPE="text/javascript">



var allowPrompt=true;

function unloadUserLock(){
IF(allowPrompt){
window.onbeforeunload = unloadPage;
}
}

function unloadPage()
{
unload();
}

function NoPrompt()
{
allowPrompt=false;
}

</script>

Best Answer chosen by Admin (Salesforce Developers) 
AmphitriteAmphitrite
Here is solution.

<script LANGUAGE="JavaScript1.2" TYPE="text/javascript">

var allowX=true;

function unloadpage(){
if(allowX){
unload();
}
else{

}
return null;
}

function setBunload(on){
window.onbeforeunload = unloadpage;
}

setBunload(true);

function clearX()
{
allowX = false;
}

</script>

All Answers

bob_buzzardbob_buzzard

This is a security feature of modern browsers - if the user wants to leave the page you can't do much other than add a custom message to the confirmation dialog.

AmphitriteAmphitrite
Here is solution.

<script LANGUAGE="JavaScript1.2" TYPE="text/javascript">

var allowX=true;

function unloadpage(){
if(allowX){
unload();
}
else{

}
return null;
}

function setBunload(on){
window.onbeforeunload = unloadpage;
}

setBunload(true);

function clearX()
{
allowX = false;
}

</script>

This was selected as the best answer
Wave TestWave Test
Can you please post complete custom code and i tried with below code and do i need to check vf page version and all i am using latest version

here is complete vf page code:

<apex:page controller="jsTestClass" >
   <script LANGUAGE="JavaScript1.2" TYPE="text/javascript">
        var allowX=true;
        function unloadpage(){
            alert('allowX');
            if(allowX){
               doAction();            
            }
            else{
            
            }
            return null;
        }
        function setBunload(on){
            window.onbeforeunload = unloadpage;
        }
        setBunload(true);
        function clearX()
        {
            allowX = false;
        }
        
    </script>

Where are you calling this setBunload method in vf page?
 
     <apex:form >
          <apex:actionFunction name="doAction" action="{!doAction}" rerender="MyPage" />
     </apex:form>
</apex:page>