6

Is it possible to get Controller property using action function?

my vf controller has property as

public String contractURL {get;set;}

I want to access this latest value in my javascript.

Note - I cannot access it in this way (and thats why I am posting this question :) )-

var url  = "{!contractURL}";

3 Answers 3

6

try putting var url = {!contractURL} in a <apex:outputPanel> and have it rerendered by your <apex:actionFunction> I don't know for sure if it will work, but give it a try!

If you want to make calls to a controller without having to rerender and use visualforce functionality, just handle the calls and results in javascript, you should take a look at Javascript Remoting: http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

4
  • This will definitely work. The only thing is I don't think you'd want to redeclare the variable, although maybe you would if it was the first, and only time you were using it or were just using it local to that reRender instant. Commented Dec 15, 2012 at 0:36
  • 1
    is there any way I can get 'contractURL' value in java script without putting it into page and do re-render ? like directly make server call and read it from there ? Commented Dec 15, 2012 at 1:13
  • 1
    take a look at Javascript Remoting: salesforce.com/us/developer/docs/pages/Content/…
    – pjcarly
    Commented Dec 15, 2012 at 13:33
  • I'll add it to my answer, as the comment helped :)
    – pjcarly
    Commented Dec 17, 2012 at 9:59
7

I know this is an old question, and it already has an accepted answer, but I recently had to do the same thing. However, I didn't want to use static method via JavaScript Remoting as I wanted to have access to some controller variables. In the end I used this simple approach and it worked:

Visualforce:

<apex:page controller="ReturnController">

    <apex:form >
        <apex:commandButton action="{!callMe}"
                oncomplete="processReturnedValue({!valueReturned}); "
                value="Click Me!"/>
    </apex:form>

    <script>
        var processReturnedValue = function(valueReturned){
            alert('Returned Value: ' + valueReturned);
        };
    </script>

</apex:page>

Controller:

public class ReturnController {

    public Integer ValueReturned {get; set;}

    public ReturnController(){
        this.ValueReturned = 0;
    }

    public void callMe(){
        this.ValueReturned++;
    }

}

That's it, that worked for me. And you could use the exactly same approach with apex:actionFunction:

<apex:actionFunction action="{!callMe}" name="actionFuncName" oncomplete="processReturnedValue({!valueReturned}); "/>
1
  • 4
    This worked for me, although if returning a string, make sure to put quotes around the {!valueReturned} part. e.g. oncomplete="processReturnedValue('{!valueReturned}');"
    – Nick C
    Commented Apr 21, 2018 at 0:39
5

You should be able to access the controller property in JavaScript that is directly in your page, such as:

var url = "{!contractURL}";

The Visualforce engine will substitute in all of the values when generating the page.

If you are updating that value in your controller as part of an ajax call (e.g., command button with reRender) it won't update the value of the JavaScript value since that part of the page was already generated.

In a part that you are reRendering you could set the value of the variable, or probably better, create a dedicated outputPanel to set it.

<apex:outputPanel id="reRenderedPanelToSetVariable">
   <script type="text/javascript">
      url = "{!contractURL}";
   </script>
</apex:outputPanel>

Then from your command button or whatever you just include the "reRenderedPanelToSetVariable" in the list of ids to rerender.

2
  • is there any way I can get 'contractURL' value in java script without putting it into page and do re-render ? like directly make server call and read it from there ? Commented Dec 15, 2012 at 0:57
  • 3
    You'd need to use JavaScript remoting for that, I think. You tend to lose the stateful nature of the page though. Commented Dec 15, 2012 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .