Roni28
Roni28

Reputation: 15

How can I pass a value from a jsp page to a javascript page?

I have this code in my jsp file:

     int hours=5;
     int minutes=3;
     int seconds=2;

<h3 id="c_down">TIME LEFT <br> <%=hours%> : <%=minutes%> : <%=seconds%> </h3>
<script src="countdown.js"></script>


This is my javascript file (countdown.js) :



let minutes=
let seconds = 
let hours= 


let timming_onSeconds=seconds+minutes*60+hours*3600;

const cd=document.getElementById('c_down');


setInterval(uptade_countdown, 1000);

function uptade_countdown() {


    hours=Math.floor(timming_onSeconds/3600);
    minutes=Math.floor((timming_onSeconds%3600)/60);
    seconds=(timming_onSeconds%3600)%60;


    seconds=seconds <= 9 ? "0"+seconds : seconds;
    minutes=minutes <= 9 ? "0"+minutes : minutes;
    hours=hours <= 9 ? "0"+hours : hours;


    cd.innerHTML = `TIME LEFT <br> ${hours} : ${minutes} : ${seconds}`;


   timming_onSeconds--;


}

How can I give the values from seconds, minutes, hours that are from jsp file to seconds, minutes, hours that are in my javascript file?

Upvotes: 0

Views: 164

Answers (1)

Anton
Anton

Reputation: 2703

You can set variables with inline script:

<h3 id="c_down">TIME LEFT <br> <%=hours%> : <%=minutes%> : <%=seconds%> </h3>
<script>
let minutes = <%=minutes%>;
let seconds = <%=seconds%>;
let hours = <%=hours%>;
</script>
<script src="countdown.js"></script>

So remove this block from countdown.js:

let minutes=
let seconds = 
let hours= 

Upvotes: 1

Related Questions