Sachin10, The order of operations for a visualForce page is very important to understand and can lead to lots of confusing results if you don't understand it. Here's an official explanation (http://www.salesforce.com/docs/developer/pages/Content/pages_controller_lifecycle_example.htm). Based on this, what you're seeing is that the constructor code executes as soon as the VF page is called, and the variables that are set at that time are available to use as {!mergeFields} in your VF page. However after the constructor has loaded, the VF page starts to render and the {!mergeFields} are output to HTML with whatever values they had initially. They are not kept dynamically updated, you have to manually cause them to update. Although there are several ways to update the {!mergeFields} in your page, such as wrapping them in tags that you then rerender (see the first accepted solution here (https://developer.salesforce.com/forums/ForumsMain?id=906F000000098hhIAA)) one nice way to update them is to call the function using an actionFunction and then to use the oncomplete event of that function to update the variable values. Assuming that we're using the controller you defined in your question above, here is the VF you would use:
You can use apex:actionFunction as it causes the onLoad part of the Javascript in the <script> tag to be re-computed. Due to this the new values of the variables from the controller are fetched into the Javascript (which were modified in any method in the controller). First click on the button "Call ActionFunction". Then click on the button "Call Javascript function".
The visualforce page :- <apex:page controller="Nov19Class1"> <apex:form> <apex:actionFunction name="af1" action="{!setToWinter}"/>
You need to reference the apex variable to javascript var (dont forget to place apex variable in single qoutes):
<script>
var jsVariable = '{!apexVariable}';
</script>
If you have case where javascript variable that is filled with apex value you can place the Script tag inside apex:outputPanel and the using rerender functionality refresh the panel alongisde assignment of the javascript variable. Something like this snippet:
Hi,
Try the below code snippet as reference:
----------- vf page----------------
<apex:page controller="accessmergefieldinjavascript" >
<script>
var stringvalue='{!s}';
var integervalue='{!nn}';
alert(' stringvalue '+stringvalue+' integervalue '+integervalue);
</script>
</apex:page>
------------ Apex controller -----------------
public class accessmergefieldinjavascript
{
public string s{get;set;}
public integer nn{get;set;}
public accessmergefieldinjavascript ()
{
s='hello';
nn=10;
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
All Answers
Hi,
Try the below code snippet as reference:
----------- vf page----------------
<apex:page controller="accessmergefieldinjavascript" >
<script>
var stringvalue='{!s}';
var integervalue='{!nn}';
alert(' stringvalue '+stringvalue+' integervalue '+integervalue);
</script>
</apex:page>
------------ Apex controller -----------------
public class accessmergefieldinjavascript
{
public string s{get;set;}
public integer nn{get;set;}
public accessmergefieldinjavascript ()
{
s='hello';
nn=10;
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Thanks Navatar it works.
Hi,
Is there any way to access controller variable set in a Method in Javascript.
In the example above the value is set in constructor.
But when I set it in a method it is showing as NULL in Javascript
In controller:
public class abc{
public string varA{get;set;}
public void fn(){
varA='hello';
}
}
In VFPage:
function hello(){
var bool = '{!varA}';
alert(bool);
}
The alert statement in the VF Page(i.e bool) is displayed as null.
Can you please help how to access a variable set from a method instead of constructor?
Many Thanks:)
The order of operations for a visualForce page is very important to understand and can lead to lots of confusing results if you don't understand it. Here's an official explanation (http://www.salesforce.com/docs/developer/pages/Content/pages_controller_lifecycle_example.htm). Based on this, what you're seeing is that the constructor code executes as soon as the VF page is called, and the variables that are set at that time are available to use as {!mergeFields} in your VF page. However after the constructor has loaded, the VF page starts to render and the {!mergeFields} are output to HTML with whatever values they had initially. They are not kept dynamically updated, you have to manually cause them to update. Although there are several ways to update the {!mergeFields} in your page, such as wrapping them in tags that you then rerender (see the first accepted solution here (https://developer.salesforce.com/forums/ForumsMain?id=906F000000098hhIAA)) one nice way to update them is to call the function using an actionFunction and then to use the oncomplete event of that function to update the variable values. Assuming that we're using the controller you defined in your question above, here is the VF you would use:
<apex:page controller="abc">
<script>
alert('hello');
var bool = '{!A}';
alert(bool);
</script>
</apex:page>
controller class
public class abc{
public string A{get;set;}
public abc(){
A='somu';
}
}
The visualforce page :-
<apex:page controller="Nov19Class1">
<apex:form>
<apex:actionFunction name="af1" action="{!setToWinter}"/>
<apex:outputPanel id="displaySeason">
<apex:outputText value="{!season}"/>
</apex:outputPanel>
<br/><br/>
<apex:commandButton value="Call ActionFunction" rerender="displaySeason" onclick="af1()"/>
<br/><br/>
<apex:commandButton value="Call Javascript function" onclick="displaySeasonJS();"/>
</apex:form>
<script>
var theSeasonIs = '{!season}';
alert('in Javascript onload, theSeasonIs is - ' + theSeasonIs);
function displaySeasonJS()
{
alert('in Javascript function displaySeasonJS() - theSeasonIs is - ' + theSeasonIs);
}
</script>
</apex:page>
The controller :-
public class Nov19Class1
{
public String season {get;set;}
public Nov19Class1()
{
season = 'Summer';
}
public pageReference setToWinter()
{
season = 'Winter';
return null;
}
}
If you have case where javascript variable that is filled with apex value you can place the Script tag inside apex:outputPanel and the using rerender functionality refresh the panel alongisde assignment of the javascript variable.
Something like this snippet:
Visualforce Page:
Controller: