2

I am trying to run javascript, that will set a value in the controller on page load. However, I cannot seem to get it to run.

Visual force page

   <apex:page controller="playGroundController" action="{!pageLoad}" standardStylesheets="false" docType="html-5.0" showHeader="false">

    <header>
        <apex:form>
            <apex:actionFunction action="{!setSite}" name="setSite">
                <apex:param name="param1" assignTo="{!site}" value="GC"/>    
            </apex:actionFunction>
            <script type="javascript">
            function run(){
                console.log('hello')
                setSite('GC')
            }
            window.onload = run;
            </script>
        </apex:form>
    </header>
    <h3>
        I am text
    </h3> 


</apex:page>

Controller

public class playGroundController {
public string site {get;set;}

public void pageLoad(){
    site = null;
}

public void setSite(){
    system.debug('set site');
    system.debug(site);
}
}
3
  • Why are you using Javascript for this functionality?
    – Adrian Larson
    Commented Jun 27, 2017 at 17:20
  • 1
    Also consider that Visualforce might have setup window.onload before your code runs so you may be breaking something in Visualforce you want to rely on now or later. If you really need to run some JavaScript put it inline at the end of the page which is after the DOM has been built.
    – Keith C
    Commented Jun 27, 2017 at 17:25
  • It has been the only way to trigger ActionFunctions without having user input as far as I found. The goal is to set the site on the controller so the same controller can be used for multiple VF pages.
    – bmcginn1
    Commented Jun 27, 2017 at 17:25

1 Answer 1

2

First, you haven't specified a reRender target (even nothing), so best case scenario is that this page would infinitely reload. Also, setting window.onload will cause problems. Here is the minimum modified version you would want to use:

<apex:page controller="playGroundController" action="{!pageLoad}" standardStylesheets="false" docType="html-5.0" showHeader="false">

    <apex:form id="form">
        <apex:actionFunction action="{!setSite}" name="setSite" reRender="form">
            <apex:param name="param1" assignTo="{!site}" value="GC"/>    
        </apex:actionFunction>
    </apex:form>
    <h3>
        I am text
    </h3> 

    <script type="javascript">
    window.addEventListener("DOMContentLoaded",
        function() {
            console.log("hello");
            setSite("GC");
        });
    </script>

</apex:page>
5
  • 1
    Isn't the DOMContentLoaded event deprecated? I thought you were supposed to use MutationObserver for that event type now.
    – Adrian Larson
    Commented Jun 27, 2017 at 17:48
  • @AdrianLarson You must be thinking of something else DOMContentLoaded isn't deprecated.
    – sfdcfox
    Commented Jun 27, 2017 at 17:54
  • Yeah I guess I was thinking of DOMNodeInserted and other Mutation Events.
    – Adrian Larson
    Commented Jun 27, 2017 at 17:57
  • 2
    Thank you for the help. I added the changes. The script still does not execute. I am checking the console on the page and it never triggers.
    – bmcginn1
    Commented Jun 27, 2017 at 18:34
  • didn't work for me either, nothing on console
    – love gupta
    Commented Jun 26, 2023 at 15:24

You must log in to answer this question.

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