Still stuck on JavaScript in Domino form

Despite a really god response to my last question on this, I am still trying to read a field value for the “seconds” to be used by this timer. I must be doing it wrong as nothng seems to work except hard coding the value. I put a field at the top of the forn and hard-coded it to a value of 55, it’s hidden by an html rem, but nothing I try gets the value.

I had a thought that the code was running before the page loaded so that the field value wasn’t available because these attempts returned “NaN” in the seconds variable, which apparently means Not a Number. Any ideas? Is it possible to call this by some other event or check that the page is loaded, or that the field value isn’t null? Maybe you could tell me the code for a js button to call the display() function so it could be tested after the page loads.

var milisec=0

//var seconds=document.forms[0].elements[SecondsAllowed].value

//var seconds=document.forms[0].elements[“SecondsAllowed”].value

//var seconds=document.forms[0].SecondsAllowed.value

var seconds=120

function display(){

if (milisec<=0){

milisec=9 

seconds-=1 

}

if (seconds<=-1){

milisec=0 

seconds+=1

}

else

milisec-=1 

document._SkillsTestQ01.TimerMsg.value="You have "+seconds +" seconds left to answer"

setTimeout("display()",100) 

}

Subject: Still stuck on JavaScript in Domino form

As I said, you can’t use the field until it loads, so any inlined JavaScript in the head (JS that isn’t in a function) can’t refer to the field even after you fix the comment problem. You need to create the variable inline so it can be used globally, but you can’t set its value:

var seconds;

function init() {

seconds=document.forms[0].elements[“SecondsAllowed”].value;

display();

}

Note that SecondsAllowed is in quotes – it’s the name of an element in the form’s elements collection.

Call init() from the onload, and let it call display(). There’s still that document._SkillsTestQ01.TimerMsg.value mess, though – it’s not legal (it works in IE by making implicit calls to the .all collection, and it will bite you eventually).

[EDIT - ADDED] It looks like that can be restated as document.forms[“_SkillsTestQ01”].elements[“TimerMsg”].value if it’s actually been working for you when you use a literal value for the seconds variable.

Subject: RE: Still stuck on JavaScript in Domino form

Stan, that’s brilliant, it’s great when a plan comes together. It tworks fantastically well. Now, about this mess on the end…

else

milisec-=1 

document._SkillsTestQ01.TimerMsg.value="You have "+seconds +" seconds left to answer"

setTimeout("display()",100) 

Two things…

  1. I would like this code not to use _SkillsTestQ1 as I have 20 forms and would have to have 20 verisons of this code. Can I get it to read the formname itself. I am guessing that if I make a copy of the line in init() that gets the seconds value , that I can get the form name from the form field, but I don’t know how to use it in the messy bit, and it does need to work on popular browsers.

Notes: The form nameis SkillsTestQ1 not _SkillsTestQ1, I got that from the web oage source view. Also, TimerMsg is a visible text field that displays the ocuntdonw message.

  1. I reallyy don’t understasnd the timout code, could you explain please

Subject: RE: Still stuck on JavaScript in Domino form

Well, assuming that there is only one form present (in other words, that you haven’t created any addition HTML elements in passthru HTML), you can simply use:

document.forms[0].elements[“TimerMsg”].value = “You have “+seconds +” seconds left to answer”;

JavaScript arrays are a sort of weird hybrid of what most languages call “arrays” and what a language like Python would call a “dictionary” – elements can be addressed using an index (in this case a zero, meaning the first element) or by a name when one is available. While there are circumstances where the name of the element will not be available, you can always use the index.

As for the setTimeout(), it tells the JavaScript runtime to wait 100 milliseconds and call the display() function again. The first argument to setTimeout() is the “what to do” argument, expressed as a string rather than as bare code, and the second is “how long to wait” in milliseconds.

Subject: RE: Still stuck on JavaScript in Domino form

Stan you are a genius!! Life is so much less compolicated when you know a man who can. Great stuff.

One slight problem, while the page is loading it shows NaN for the seconds in the message. not sure why it shows the message at all until the page loads. The full code looks like this

var milisec=0

var seconds;

function init() {

seconds=document.forms[0].elements[“SecondsAllowed”].value;

display();

}

function display(){

if (milisec<=0){

milisec=9 

seconds-=1 

}

if (seconds<=-1){

milisec=0 

seconds+=1

}

else

milisec-=1 

document.forms[0].elements["TimerMsg"].value = "You have "+seconds +" seconds left to gain a bonus point";

// document._SkillsTestQ01.TimerMsg.value=“You have “+seconds +” seconds left to gain a bonus point”

setTimeout("display()",100) 

}

Stan you mentioned PHP, I am looking for someone with an understanding of PHP (and various other langages) for a commission paid project, I don’t suppose you would be interested? It doesn’t take very long to do. If you would like to know more, send me a quick email, and I will explain.

iqnetsys@hotmail.co.uk

Subject: RE: Still stuck on JavaScript in Domino form

Looks like it isn’t reading the seconds value as a number, so modify the line in init() to read:

seconds=parseInt(document.forms[0].elements[“SecondsAllowed”].value);

Subject: RE: Still stuck on JavaScript in Domino form

Stan, your making this too easy for me. A perfect answer once again. Thanks

Subject: RE: Still stuck on JavaScript in Domino form

it’s hidden by an html rem

If it’s in a comment, it’s a COMMENT, not part of the form. The whole point of a comment is that it’s non-functional text – not just invisible to the web user, it might as well not be there at all.

Take the field out of the comment and just make it type hidden instead.

Subject: RE: Still stuck on JavaScript in Domino form

Andre, Thanks. I know I really should have thought of that. thanks again, it helped in the final fix.