+ Start a Discussion

how to access controller variable from visual force page's java script

how to  access controller variable from visual force page's java script
Ron HessRon Hess

You can do this by using the $Component feature of Visualforce to generate document ID's. 


So first you bind the method or property to a visualforce expression, then you peek or poke that value in the DOM to access the information that came from the controller.


Here is an example, the code is < script on a Visualforce page, and it accesses a method / property called "kml" or getKml() in the controller.


Note the outputText is happening right inside the script block :smileysurprised:



<script> google.load("earth", "1");
var ge = null;

function init() {
google.earth.createInstance('{!$Component.map3d}', initCallback, function (object) { } );

function initCallback(object) {
ge = object;

var p = ge.parseKml( "<apex:outputText value="{!kml}" escape="false" />" );

var la = computeFitLookAt(ge, p, 1.4 );
if ( la != null ) {








Could you please give one more simpler example of doing this..


I have one variable in javascript. Need to assign the value of the Javascript variable to one class variable.

How to do this?? Please suggest..


Thanks in advance,