Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

TOPIC: javascript vs php (EM) timestamps

javascript vs php (EM) timestamps 3 years 11 months ago #89870

  • Nico01
  • Nico01's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
Hi,

a friend asked me to pimp his survey. One of the requirements was to add a global timer for a group of questions in order to stress the subject.

My first solution was to created an equation that stores a timestamp and then access it from javascript to compute the time difference. But equation functions use php, which runs on the server with a different time from the client.

Then I tried:
<script type="text/javascript" charset="utf-8"> 
jQuery(document).ready(function() {
document.write("{" + parseInt(new Date().getTime()/(1000)) + "}");
});
</script>
But when looking at the question's source code, I realized that brackets are pre-processed.

Then I did:
<script type="text/javascript" charset="utf-8"> 
jQuery(document).ready(function() {
document.getElementById('answer847544X185X1603').value = parseInt(new Date().getTime()/(1000));
});
</script>
This solution has one major problem, I cannot hide the question otherwise the answer isn't set.

At this point I disable the next button, hide the textarea, update the textarea and then skip to the next question.

Is there a better solution to this problem ?

Thank you in advance !
The administrator has disabled public write access.

javascript vs php (EM) timestamps 3 years 11 months ago #89877

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1894
  • Thank you received: 467
  • Karma: 118
Hello,
try adding a white-space after the bracket
document.write("{ "

Ben/
BenoƮt

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
All LS releases => bit.ly/1VMuTDu | 2.06lts => bit.ly/1Qv44A1
Demo surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The administrator has disabled public write access.

javascript vs php (EM) timestamps 3 years 11 months ago #89934

  • Nico01
  • Nico01's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
I tried your proposal, it leads to a weird behavior: it produces a white page with the expected text output and without the usual survey decorations.

But I still cannot access the data.
The administrator has disabled public write access.

javascript vs php (EM) timestamps 3 years 11 months ago #89936

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6566
  • Thank you received: 1477
  • Karma: 578
Expression Manager parses everything inside curly braces without preceding and following spaces, so what Ben meant was adding a space after the opening brace and a space before the closing brace:
document.write("{ " + parseInt(new Date().getTime()/(1000)) + " }");
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.
The following user(s) said Thank You: Ben_V

javascript vs php (EM) timestamps 3 years 11 months ago #89938

  • Nico01
  • Nico01's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
Well, that's exactly what I tried...
However this isn't a solution.

I want to store a javascript timestamp and access it later. But LimeSurvey's behaviour is to pre-process the curly braces and its content on the server. Then if I print these from javascript, on the client, the server will never know about it.
The administrator has disabled public write access.

javascript vs php (EM) timestamps 3 years 11 months ago #89940

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6566
  • Thank you received: 1477
  • Karma: 578
When you say "I cannot hide the question otherwise the answer isn't set.", how are you hiding the question?

You should be able to hide it with JS and still manipulate it. Something like this for a short-text question:
<script type="text/javascript" charset="utf-8"> 
	jQuery(document).ready(function() {
		var qID = 1603
		$('#question'+qID+).hide();
		$('#question'+qID+' input.text').val(parseInt(new Date().getTime()/(1000)));
	});
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

javascript vs php (EM) timestamps 3 years 11 months ago #89942

  • Nico01
  • Nico01's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
When you say "I cannot hide the question otherwise the answer isn't set.", how are you hiding the question?

With the option in the advanced parameters, as I do when using equations.

What you proposed is what I said I did at the end of my first post in the topic.

However, the line "$('#question'+qID+).hide();" gave me the idea to try "$('#limesurvey').hide();". But an empty box is still shown.
The administrator has disabled public write access.

javascript vs php (EM) timestamps 3 years 11 months ago #89943

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6566
  • Thank you received: 1477
  • Karma: 578
With the option in the advanced parameters, as I do when using equations.
Ah, that param prevents the question from being rendered (which I don't understand) so it can't be manipulated with JavaScript. You'll need to hide it with JS or CSS.
However, the line "$('#question'+qID+).hide();" gave me the idea to try "$('#limesurvey').hide();". But an empty box is still shown.
If you want to hide the whole page while you auto-submit:
$('body').hide()
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.
The following user(s) said Thank You: Nico01
Time to create page: 0.235 seconds
Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form