How to apply JavaScript function to a Caspio datapage.

bfuchs used Ask the Experts™
Hi Experts,

This question is related to this suggestion.

How do I apply that to a Caspio datapage?

See attached how its possible to insert JS code in Caspio's design model.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
you can put he following just after the </select> tag and before the <script> tag :

document.addEventListener('DataPageReady', function (event) {

    function mil(str) {
        var t = str.split(':')
        var hh = parseInt(t[0],10);
        var isAM = str.toLowerCase().indexOf('am')!=-1;
        hh -= (isAM && hh==12)?12:0;
        hh += (isAM)?0:12;
        if (hh<10) hh="0"+hh;
        return ""+hh+':'+t[1];

    var Shift_From_Hour = $("select[id*=Shift_From_Hour]").val();
    var Shift_From_AMPM = $("select[id*=Shift_From_AMPM]").val();
    var Shift_To_Hour = $("select[id*=Shift_To_Hour]").val();
    var Shift_To_AMPM = $("select[id*=Shift_To_AMPM]").val();

    var mil1 = mil(Shift_From_Hour + Shift_From_AMPM);
    var mil2 = mil(Shift_To_Hour + Shift_To_AMPM);
    var arr1 = mil1.split(":");
    var arr2 = mil1.split(":");
    var mil1_in_millisecond = (mil1[0]*3600 + mil1[1]*60) * 1000;
    var mil2_in_millisecond = (mil2[0]*3600 + mil2[1]*60) * 1000;
    var isSameDay = mil1_in_millisecond < mil2_in_millisecond; // true of false

    var Visit_Date = $(":text[id*=Visit_Date]").val(); // mm/dd/yyyy
    var arr3 = Visit_Date.split("/");
    var visit_date = new Date(arr3[2], arr3[0]-1, arr3[1]).getTime();

    var Shift_Start_Date = visit_date + mil1_in_millisecond ;
    if(isSameDay) {
        var Shift_End_Date = visit_date + mil1_in_millisecond;
    } else {
        var Shift_End_Date = visit_date + mil2_in_millisecond + 24 * 3600 * 1000;

Open in new window

Thank you!
Top Expert 2014

you welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial