data-script

Executes given javascript code when an event occurs.

Available events: data-script-before, data-script-complete, data-script-success (default), data-script-failure, data-script-empty, data-script-not-empty


data-script-(event) option allows to execute custom javascript code when an event occurs. You can compare it to onclick, onchange and so on... attributes, but specific for domajax events. If the executed javascript code returns false, domajax will stop processing your ajax call.

Your code receives the same context as data-callback, elem (the jQuery element that contains the ajax call), response, textStatus, jqXHR (the 3 arguments given by jQuery.ajax to success/error callbacks, set to undefined on before/complete domajax callbacks).

Note that data-script is called before data-callback but after data-confirm. If your script is too long, use data-callback instead for better code readability.

data-script has no default event, you should use data-script-(event) instead.

Usage


  • data-script="(JavaScript code)"

Example


  • data-script="alert('Hello, world!');"

Live examples

This form looks like this:


<input
        type="button"
        value="Click me to receive some alerts..."
        class="btn domajax click"
        data-endpoint="script-alert-handler.php"
        data-script-before="alert('Ajax call will begin.');"
        data-script-success="alert('Ajax call has succeeded! Result is ' + response);"
        />

This form looks like this:


<div id="wait" style="display:none;">
    <center>
        <img src="../img/wait.gif" alt="Please wait"/>
    </center>
</div>

<input
        type="button"
        value="Click here to wait 3 secs."
        class="btn domajax click"
        data-endpoint="script-wait-handler.php"
        data-script-before="$('#output').html($('#wait').html());"
        data-output="#output"
        />

<div id="output"></div>

This form looks like this:


<input
        type="button"
        value="Get the current time"
        class="btn domajax click"
        data-endpoint="script-alert-handler.php"
        data-script-before="return confirm('Do you want to process the ajax call ?');"
        data-output="#output"
        />

<div id="output"></div>


See also    data-callback 

Table of contents

All the documentation at a glance

Domajax options

And more with domajax