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 

onbeforeunload conditional? [prevent from running if commandbutton is clicked.

This script code triggers the unload actionfunction appropriately. However, I need it to not trigger if the saveClose or saveNext command buttons are hit. Any suggestions?

 

 

<script>
window.onbeforeunload = unloadPage;
function unloadPage(){unload();}

</script>


<apex:actionFunction name="unload" action="{!unload}" rerender="MyPage" />

<apex:commandButton value="Save and Close" action="{!saveClose}"/>
<apex:commandButton value="Save and Next" action="{!saveNext}"/>

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>