How to make Javascript works in a form!?

Re: How to make Javascript works in a form!?

extremely simple case:

Method javascript -> name “metmet”, add code:


var r = 'help';
return r;

Javascript -> onchange of a field X:

var t = metmet();
alert("1: "+t);

When running the form, and changing the value in field X, I get an alert showing the text “1: help”;

It’s not as complex as your case, but maybe it’s a start.

Re: How to make Javascript works in a form!?

Ok, I have my script at Method javascript --> difftime
From Javascript -> onchange I add - difftime; (what fields shell I choos?)

I can see the “Processing” pop up but nothing happen!

Re: How to make Javascript works in a form!?

hi Freezer

I have tested your script but I changed the “var t = metmet();” to “var t = difftime();” so it will call my script. But then I got “1: undefined

Could it be that something is wrong with my script? And that’s why it’s not working?

Re: How to make Javascript works in a form!?

Hasamba,

I have not time to debug your code. I suggest You first create a simple case, like mine, and then keep adding pieces of code until it stops working.

I did find out that if the method javascript contains the alert function, it stops working… In my case I return a value and start the alert from within the event. Good luck.

Re: How to make Javascript works in a form!?

Thanks you all so far.

This is really weird!! It seems like no body knows how to do that in SC Form. Or maybe it’s too complicated.
I really want it to use ScriptCase, but if I can’t do that in ScriptCase, then I will have to look for another App.

Cheers.

Re: How to make Javascript works in a form!?

Hi Freezer and Scott

I figure out what is the problem!! I have a function inside a function.
When I put my script under Method javascript -> ‘timediff’ It creates a function called ''timediff"
Now, when I call the function ''timediff" from Javascript -> onclick of a field ‘total’ nothing happens because, inside my script I have other functions; chkstart () and calcDiff () which are ignored!!! :slight_smile: I have learned that from http://hungred.com/how-to/tutorial-function-function-javascript/
Now that we know what is the problem, how can I can solved it? Do you have any ideas?

This is the script :

var st;
var stsplt;
var ftsplt;
var passflag;

function chkstart () {
var flag = 0;
st = document.myform.stime.value;
st = st.replace(/[.-]/,":");
if (!/d{1,2}:d{2}/.test(st)) {
flag ++;
}

stsplt = st.split(":");
stsplt[0] = stsplt[0] * 1;
stsplt[1] = stsplt[1] * 1;

if ((stsplt[0] < 0) || (stsplt [0] > 23)) {
flag ++;
}
if ((stsplt[1] < 0) || (stsplt [1] > 59)) {
flag ++;
}

if (flag > 0) {
alert ("Invalid time - please re-enter! ");
flag = 0;
document.myform.stime.value = "";
document.myform.stime.focus();
return false;
}
else {
passflag = 1;
}
}

function calcDiff () {
if (passflag != 1) {
alert ("You must enter a starting time!" );
document.myform.stime.value = "";
document.myform.stime.focus();
return false;
}
var flag = 0;
var ft = document.myform.etime.value;
ft = ft.replace(/[.-]/,":");
if (!/d{1,2}:d{2}/.test(ft)) {
flag ++;
}
ftsplt = ft.split(":");
ftsplt[0] = ftsplt[0] * 1;
ftsplt[1] = ftsplt[1] * 1;
if ((ftsplt[0] < 0) || (ftsplt [0] > 23)) {
flag ++;
}
if ((ftsplt[1] < 0) || (ftsplt [1] > 59)) {
flag ++;
}
if (ftsplt[0] < stsplt[0]) {
flag ++;
}
if ((ftsplt[0] == stsplt[0]) && (ftsplt[1] > stsplt[1])) {
flag ++;
}

if (flag > 0) {
alert ("Invalid time - please re-enter! ");
flag = 0;
document.myform.etime.value = "";
document.myform.etime.focus();
return false;
}

var hrsDiff = ftsplt[0] - stsplt[0];

if (ftsplt[1] < stsplt[1]) {
hrsDiff = hrsDiff -1;
alert (hrsDiff)
}

var minsDiff = ftsplt[1] - stsplt[1];
if (minsDiff < 0) {
minsDiff = 60 + minsDiff;
}
if (minsDiff < 10) {
minsDiff = "0" + minsDiff;
}

var diffTime = hrsDiff + ":" + minsDiff;
document.myform.total.value = diffTime;

}

Re: How to make Javascript works in a form!?

I’m afraid that’s not the problem. When I change my method to this it still works. A function within a “method javascript” is ok.


var r = 'help';
function change_string() {
 return " me";
}
var t = r + change_string();
return t;

In your code the alert messes things up (don’t ask me why).
Furthermore i think this code is not correct:


...document.myform.startTime.value....

… because SC generates forms with names like F1, F2 etc. Does the form myform exist in your application?

Re: How to make Javascript works in a form!?

Hi Freezer

Yes, I know about the form name. I tried to change that to F1 (and also to give my own name to the form) but it didn’t change anything. Do I have to have the name of the form in the script?
But also I wondered about my fields names. How SC register the names? Is it like ID or …else. Because the script is looking for fields with the ID = ‘name’. Maybe this also one of the problems!!

Re: How to make Javascript works in a form!?

check the source code of the SC application to find the form and field names.

Re: How to make Javascript works in a form!?

Nop, couldn’t find ‘ID’ :-/ I’m not really sure if a normal Javascript will work in that situation!!
I hope someone will prove me wrong. Maybe SC form is not design to do that function, because I really don’t know how SC expect me to do it!

Re: How to make Javascript works in a form!?

here’s some compact code to calculate difference between two dates in hh:mm format.
Maybe you can use it. Works in my SC application.


var somedate1 = new Date("October 13, 1975 10:00:00");
var somedate2 = new Date("October 13, 1975 15:59:59");

var diff_minutes = Math.floor((somedate2.getTime()-somedate1.getTime())/(1000*60));
var hours = Math.floor(diff_minutes/60);
var minutes = diff_minutes % 60;

return hours+":"+minutes;

Re: How to make Javascript works in a form!?

Shell I put it like this under Javascript => onClick?

var stime = new Date();
var etime = new Date();

var diff_minutes = Math.floor((etime.getTime()-stime.getTime())/(1000*60));
var hours = Math.floor(diff_minutes/60);
var minutes = diff_minutes % 60;

return hours+":"+minutes;

Re: How to make Javascript works in a form!?

What kind of application/form are you building?
how and when do you want to calculate the difference in time? and for what fields?

Re: How to make Javascript works in a form!?

The form is a Single record (name: form_jos_log), and I want to calculate when the user click away from the second time record (etime) or on the “total” field. The fields names are:
‘stime’, ‘etime’ and ‘total’.

Re: How to make Javascript works in a form!?

Hi Freezer

When I use this code: It’s working.
var stime = new Date(“October 13, 1975 10:00:00”);
var etime = new Date(“October 13, 1975 20:00:00”);

But when I try to use this code: To retrieve the time from my fields, it’s not working!
var stime = new Date(“id_sc_field_stime”);
var etime = new Date(“id_sc_field_etime”);

I have also tried to use: But nothing happen!!
var stime = document.getElementById(“id_sc_field_stime”).value
var etime = document.getElementById(“id_sc_field_etime”).value

Is it wrong the why I’m trying to retrieve the times from the fields?

Re: How to make Javascript works in a form!?

Check what value id_sc_field_stime returns.
Convert this value to a string that is accepted by javascript Date() function (google).

Re: How to make Javascript works in a form!?

He Freezer,
I really don’t know how to do that. I have tried all kinds of combination but nothing.
If you don’t have time to help me, it ok.

Cheers.