M
M
MaxQw2020-08-06 18:50:45
JavaScript
MaxQw, 2020-08-06 18:50:45

How to correctly pass {{ var }} OctoberCMS to javascript?

Replace in given js (active hours), var today = new Date(); values ​​from {{ var }} ?

Something like:

{% set date = "now"|date("H:i:s d.m.Y") %}
{% set hour = date|date("H") %}
{% set min = date|date("i") %}
{% set sec = date|date("s") %}


Paste in https://www.w3schools.com/js/tryit.asp?filename=tr...

<script>
function startTime() {
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  m = checkTime(m);
  s = checkTime(s);
  document.getElementById('txt').innerHTML =
  h + ":" + m + ":" + s;
  var t = setTimeout(startTime, 500);
}
function checkTime(i) {
  if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
  return i;
}
startTime();
</script>


Replaced with

var h = '{{ hour }}'
  var m = '{{ min }}'
  var s = '{{ sec }}'


The clock is no longer active.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pashok Doom, 2020-08-06
@MaxQw

Bring it to hidden inputs and pull in the script by IDs. If the script is directly in the template, output it there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question