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
Alx MrAlx Mr 

javasript to call the apex function

I'm not able to call an apex function that checks if attahcment was added from JavaScript.

This is wat i have:

Js:
window.addEventListener("beforeunload", function(){CheckAttachment();});
// onbeforeunload = CheckAttachment;    
function CheckAttachment(){NoAttch();}
VF
<apex:form id="TheForm">
        <apex:actionFunction name="NoAttch" action="{!NoAttachmentEmail}"/>
Controller:
public controllerInfo(){

isattch = false;
altid = false;

....
Function:
public void NoAttachmentEmail(){
        if (isAttch==false && altId == false){
.....}
}

Do i still miss anything? 

Thank you very much.








 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) https://developer.salesforce.com/forums/?id=906F00000008zaMIAQ
2) http://salesforce.stackexchange.com/questions/4910/calling-a-apex-controller-method-using-javascript-in-vf-page
 
<apex:page controller="t">
<script>
    function myJavascriptFunc()
    {
        alert('Entered Javascript') ;
        CallApexMethod() ;
    }
</script>
<apex:form >
<apex:actionFunction name="CallApexMethod" action="{!myActionInController}" onComplete="alert('After apex method') ;"/>
  <apex:pageBlock >
        <apex:pageBlockButtons>
            <apex:commandButton value="Hit Me" onclick="myJavascriptFunc() ;"/>
        </apex:pageBlockButtons>
  </apex:pageBlock>
</apex:form>
</apex:page>
 
public class t
{
    public PageReference myActionInController()
    {
        return null ;
    }
}



 
Alx MrAlx Mr
Thank you Amit,
This is helpful.

However, I need to run function on beforeunload, and I do not manage to have it working.