Storing a value created using javascript as a value (SSI Web v7)

I want to ascertain the current day of the week for each respondent.  I have found the following piece of code on w3schools, which works ... but is there a way for me to store (and of course use) the result?  I've tried creating a free format question, but just not sure what to set as the value.

Here's the code from w3Schools ...

<script type="text/javascript">

var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

document.write(weekday[d.getDay()]);

</script>
asked Nov 3, 2011 by Marion (1,292 points)
edited Nov 4, 2011 by Walter Williams

2 Answers

+1 vote
 
Best answer
create a freeformat hidden variable and set the value using javascript.

<input name="freeformat_today" id="freeformat_today" type="hidden">

<script type="text/javascript">

var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

document.getElementById("freeformat_today").value = weekday[d.getDay()];

</script>


or you can use unverified perl to achieve it.  The advantage of perl is that the value is computed as the page loads not after.  Also it will take the date from the server not the client.  This is a catch 22.  If the server has the correct date which is the case 99% of the time then there is no problem.  The javascript will take the time from the client machine.

<input name="freeformat_today" id="freeformat_today" type="hidden" value="
[% Begin Unverified Perl

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
my @WeekDays = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

return $WeekDays[$wday];

End Unverified %]
">
answered Nov 3, 2011 by Mike Lodder Silver Sawtooth Software, Inc. (6,712 points)
edited Nov 3, 2011 by Walter Williams
Thanks heaps to you both!!  Bahadir and Mike to the rescue again <grin>.

This is really good as the client can now decide whether he wants the day to be captured from the respondent's machine or from the server.  And it gives me some valuable insight into storing variables using Javascript.

Happy days :-)
+1 vote
just access your variable via document.getElementById and set its value. the code below works for a free format question named hidDay and hidden variable today.
<input name="hidDay_today" id="hidDay_today" type="hidden">

<script type="text/javascript">

var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

document.write(weekday[d.getDay()]);
document.getElementById("hidDay_today").value = weekday[d.getDay()];
document.write(document.getElementById("hidDay_today").value);
</script>
answered Nov 3, 2011 by Bahadir Ozkurt Gold (10,016 points)
edited Nov 3, 2011 by Bahadir Ozkurt
...