+ Start a Discussion
George M AlbrechtGeorge M Albrecht 

Javascript Popup on VF Page from Static Resource

Hello,

New to using JS, we have a need on a VisualForce page when the label of a pageBlockSectionItem is clicked, to execute a JS popup. In order to keep the code clean, I have put the JS in a seperate static resource. I am now trying to access a function for the onLabelClick and it is not working. If I put the <script> tags in the VF page and invoke the fuction onLabelclick it works, but is there a way to avoid that?

Below is the VF code I have that is not working:


<apex:page standardController="QA__c">
 <apex:includeScript value="{!$Resource.QA_Javascript}"/>  
 <apex:form >
 <apex:pageMessages />
 <apex:pageBlock title="QA Scoring Form">
 <apex:pageBlockButtons >
     <apex:commandButton action="{!save}" value="Save" />
     <apex:commandButton action="{!cancel}" value="Cancel"/>
 </apex:pageBlockButtons> 
 <apex:actionRegion >
  <apex:pageblockSection title="QA Information">
      <apex:inputField value="{!QA__c.Reviewee__c}"/>
      <apex:inputField value="{!QA__c.Complexity__c}"/>
      <apex:inputField value="{!QA__c.QA_Quote_Number__c}"/>
      <apex:inputField value="{!QA__c.QA_Date__c}"/>
      <apex:inputField value="{!QA__c.Overall_Comments__c}"/>
  </apex:pageblockSection>
  <apex:pageBlockSection title="Phone Etiquette" id="pe">
       <apex:pageBlockSectionItem onLabelclick="appGreet();" >       
           <apex:outputLabel value="Appropriate Greeting" for="ag1"/>
           <apex:panelGrid columns="2">
               <apex:inputField value="{!QA__c.Appropriate_Greeting__c}" id="ag1">
                <apex:actionSupport event="onchange" reRender="pe"/>
               </apex:inputfield>
               <apex:inputField value="{!QA__c.App_Greet_Comment__c}" id="ag2" label="Comments"/>
           </apex:panelGrid>
       </apex:pageBlockSectionItem>


Ravikant Saini 1Ravikant Saini 1
Just try this,
<apex:includeScript value="{!URLFOR($Resource.QA_Javascript)}"/>


George M AlbrechtGeorge M Albrecht
That did not change the result unfortunately Ravikant. It works if I just put the function in the <script> tags on the page, but trying to avoid that. 
Kiran  KurellaKiran Kurella
It looks like you are referencing controller variables in your JavaScript. Variables from the controller can't be accessed within static resource. You need to declare the JavaScript variables in VF page. For example, to access a controller variable accountId in a static resource, we need to declare it before the script declaration as follows:

<script>
var accountId = '{!JSENCODE(accountId)}';
</script>

// you will be able to access accountId variable in your static resource now
<apex:includeScript value="{!$Resource.QA_Javascript}"/>

If the problem still exists, then please post your JavaScript code so that the community can better assist you.