Skip to content
Advertisement

How to use document.write() with a file instead of just text?

I am using socket.io to create a party game similar to cards against humanity. I am just wondering how I can keep the players name and score etc without having to send all the data to a new page when new games begin. I was thinking to just change the body of the html but stay on the same page so the javascript doesnt reset and I can still access all the players information.

I came across using document.write() however this appears to only accept a string parameter. Is there a way to do something like this document.write(game.html).

Any ideas on how to go about this, maybe a better way to pass information between javascript files without having to post the data each time a new page is loaded? I have tried sending the data with post however it makes it far more difficult to keep everything consistent.

P.S This is the first time I have properly made anything with node.js, socket.io, javascript etc. So I could be asking a stupid question.

Advertisement

Answer

Once you get socket.io data from an event listener, you can use querySelector() or any other DOM manipulation method to show the data in the DOM. Something like this?

HTML

<div id="panel"></div>

JS

const socket = io("ws://localhost:3000" )
const panel = document.querySelector("#panel")
// receive a message from the server
socket.on( "hello", (arg) => {
    panel.innerText = arg
});
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement