Problem with writting code of javascript in esp8266

Hii
I am having problem with javascript code in esp8266 web server

Is there someone who can help me...

how to write javascript code inside client.println("");


 client.println("<script>let btn = document.querySelector('button');");
  client.println("document.body.addEventListener(\'keydown\', (e) =>{if(e.key==\'a\'||e.key==\'4'){window.location.href=\"/left\"\";}})</script>");

  client.println("</body>");
  client.println("</html>");

You have not escaped the second single quote after the number 4.
Try this:
client.println("document.body.addEventListener(\'keydown\', (e) =>{if(e.key==\'a\'||e.key==\'4\'){window.location.href=\"/left\"\";}})</script>");

That's all I can say; as I mentioned in our private exchange, I have no interest in jscript.

I'll alert this post to the moderators to ask them to move it to a more appropriate place on this forum, as this doesn't look like a covid-19 response issue.

Also, please give that link I sent you another very thorough read as you've only posted a snippet of your full code, making it impossible for people to help you in a constructive way.

Good luck once again with your project.

What's the problem?

The code you posted is essentially how it is done. The arduino code does not know that there is JavaScript or html inside the strings that are printed, they are just strings. When your browser receives the page, it executes the html/JavaScript.

The whole code is working fine but only problem is with script code....

A better/more detailed description of what goes wrong would be beneficial.

Btw, since it seems to be a specific JavaScript problem, this forum may not be the best place to get help.

Have you tried just hardcoding the jScript code in a file and running that in your browser directly? I'd start from there, get it to work, and then parse it into your C/C++ Arduino code. Keep in mind the escape character issue I pointed out earlier.

1 Like

Your topic was MOVED to its current forum category as it is more suitable than the original

1 Like

I am new here.
And the documentation made on arduino forum is based on old design thatswhy I messaged you directly.
Anyways Thanks for the help

Ok, I see the issue. No problem, seems like you got to a point now where people can try and help you out. I hope it'll work.

1 Like

yes...
is working fine..

If you use your web browser to evaluate the source code of the page that your ESP8266 generates, does the jScript code still look good?

1 Like

its showing this but when I remove the code from esp and arrange in certain way in th form of html and css it work fine

Like I said, look at the source code of that page in your browser. The error on the right hand side suggests you may indeed have an escape character problem as I've mentioned several times.

1 Like

I think the two double-quotes after "/left"" might be the problem.

1 Like

yeah thats right but how to write in client.println("");

How about change:
\"/left\"\"
into
\"/left\"

1 Like

Thank You guys
I just update the code and its working fine...
Thankyou for helping me...........

Aweseome, great to hear!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.