/    Sign up×
Community /Pin to ProfileBookmark

how to use a C variable or function in the javascript?

Hello,

I am working on lwip webserver. The code for the webserver is in C language. I want to create a dynamic webpage that displays the value of the 32-bit system register according to my application.
In my case, I have created a const char [ ] that has html code inside and I pass it in tcp_write( ) and the web page gets displayed! It works. I have also created javascript code in the html code itself that dynamically changes the content on the webpage through button onclick event. However, I want this to be change using the C variable in my code. So, how do I link the C global variable or local variable to the javascript?

In simpler way, I tried to declare a global variable say UINT a=10. Then, on button onclick event, when javascript is executed, I tried to change the content on webpage using the variable “a”. But it doesn’t work!

Does anyone knows how to make my C code and javascript code work together?

For reference, I am attaching the html and javascript code below:

const char testdata[]= “<html>
<body>
<button onclick=”start()”> Start the countdown </button>
<p id=”demo”> This the main page for the test application of the web server…</p>
<p id=”ch1″> Change me with variable a…</p>
<div id=”bip”>
</div>

<script>
var counter = 10;
var intervalId = null;
function finish()

{
clearInterval(intervalId);
document.getElementById(“bip”).innerHTML = “THE END!”;
if(counter == 0) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
document.getElementById(“demo”).innerHTML =
this.responseText;
}}
}
xhttp.open(“GET”, “ajaxinfo.txt”, true);
xhttp.send();
}

function bip()

{
if(counter == 0) finish();
else {
document.getElementById(“bip”).innerHTML = counter + ” seconds remaining”;
}
counter–;

}
function start() { intervalId = setInterval(bip, 1000);}
</script>
</body>
</html>”;

I want to replace any <p> element with a global variable that will be declared in the C code that has this html code inside a const char array as shown above.

Any help is highly appreciated.

Thanks,

to post a comment
Full-stack DeveloperHTMLJavaScript

0Be the first to comment 😎

×

Success!

Help @sgandhi97 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...