Run JavaScript on Form Load – WS Form

Run JavaScript on Form Load

Using Your Own JavaScript

If you need to run JavaScript when a form loads, you can use the wsf-rendered WS Form event. This will ensure that the form has loaded and rendered fully before your JavaScript runs.

The wsf-rendered event fired on the document object and is passed the form object, form_id and instance_id. You can use these parameters to determine whether your JavaScript should run.

An example of running JavaScript on form load is shown below:

(function($) {

    // Create wsf-rendered event handler
    $(document).on('wsf-rendered', function(e, form, form_id, instance_id) {

        // The form ID this script should run for
        var my_form_id = 1;

        // Do not run this JavaScript if this event does not belong to my form ID
        if(form_id != my_form_id) { return; }

        // My JavaScript here ...

    });

})(jQuery);

Using Conditional Logic

You can also run JavaScript using conditional logic. And example of doing this is shown below:

WS Form - Run JavaScript on Form Load

An advantage of using this method is that you can use WS Form variables in your Javascript.