Welcome, Guest
Username: Password: Remember me

TOPIC: javascript vs php (EM) timestamps

javascript vs php (EM) timestamps 1 year 10 months ago #89870

  • Nico
  • Nico'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.

Re: javascript vs php (EM) timestamps 1 year 10 months ago #89877

  • Ben_V
  • Ben_V's Avatar
  • OFFLINE
  • Platinum Lime
  • Posts: 1116
  • Thank you received: 251
  • Karma: 78
Hello,
try adding a white-space after the bracket
document.write("{ "

Ben/
Benoît

goo.gl/Bw5iM => Recherche GG dans le forum français (remplacer "exemple" dans la barre de recherche)
goo.gl/WX8PH => GG search for english forum (Replace "example" in the search bar)
goo.gl/IxiGu => Búsqueda en el foro en español (Cambiar "ejemplo" en la barra de...
The administrator has disabled public write access.

Re: javascript vs php (EM) timestamps 1 year 10 months ago #89934

  • Nico
  • Nico'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.

Re: javascript vs php (EM) timestamps 1 year 10 months ago #89936

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4271
  • Thank you received: 806
  • Karma: 369
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: Ben_V

Re: javascript vs php (EM) timestamps 1 year 10 months ago #89938

  • Nico
  • Nico'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.

Re: javascript vs php (EM) timestamps 1 year 10 months ago #89940

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4271
  • Thank you received: 806
  • Karma: 369
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: javascript vs php (EM) timestamps 1 year 10 months ago #89942

  • Nico
  • Nico'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.

Re: javascript vs php (EM) timestamps 1 year 10 months ago #89943

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4271
  • Thank you received: 806
  • Karma: 369
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: Nico
Moderators: ITEd
Time to create page: 0.162 seconds
Donation Image