Categories
JavaScript Answers

How to reconnect to WebSocket after close connection with JavaScript?

Sometimes, we want to reconnect to WebSocket after close connection with JavaScript.

In this article, we’ll look at how to reconnect to WebSocket after close connection with JavaScript.

How to reconnect to WebSocket after close connection with JavaScript?

To reconnect to WebSocket after close connection with JavaScript, we create a new WebSocket object when the connection is closed.

For instance, we write

const startWebsocket = () => {
  let ws = new WebSocket("ws://localhost:8080");

  ws.onmessage = (e) => {
    console.log("websocket message event:", e);
  };

  ws.onclose = () => {
    ws = null;
    setTimeout(startWebsocket, 5000);
  };
};

startWebsocket();

to define the startWebsocket function.

In it, we create a new WebSocket object to start the connection.

Then we set its onclose property to a function that calls startWebsocket to restart the connection.

Conclusion

To reconnect to WebSocket after close connection with JavaScript, we create a new WebSocket object when the connection is closed.

Categories
JavaScript Answers

How to manually reload Google Map with JavaScript?

Sometimes, we want to manually reload Google Map with JavaScript.

In this article, we’ll look at how to manually reload Google Map with JavaScript.

How to manually reload Google Map with JavaScript?

To manually reload Google Map with JavaScript, we call the trigger method.

For instance, we write

google.maps.event.trigger(map, 'resize');

to call trigger to trigger the resize event on the map to reload the map.

Conclusion

To manually reload Google Map with JavaScript, we call the trigger method.

Categories
JavaScript Answers

How to insert content into iframe with JavaScript?

Sometimes, we want to insert content into iframe with JavaScript.

In this article, we’ll look at how to insert content into iframe with JavaScript.

How to insert content into iframe with JavaScript?

To insert content into iframe with JavaScript, we use the write method.

For instance, we write

const doc = document.getElementById("iframe").contentWindow.document;
doc.open();
doc.write("Test");
doc.close();

to select the iframe with getElementById.

Then we get its document object with contentWindow.document.

Next, we call open to open the document.

We call write to write the content we want into the iframe’s document.

And then we call close to finish writing.

Conclusion

To insert content into iframe with JavaScript, we use the write method.

Categories
JavaScript Answers

How to use a case and switch statement with two variables with JavaScript?

Sometimes, we want to use a case and switch statement with two variables with JavaScript.

In this article, we’ll look at how to use a case and switch statement with two variables with JavaScript.

How to use a case and switch statement with two variables with JavaScript?

To use a case and switch statement with two variables with JavaScript, we use switch with true.

For instance, we write

switch (true) {
  case var1 === true && var2 === true:
    //...
    break;
  case var1 === false && var2 === false:
    //...
    break;

  default:
}

to use switch with true.

Then the case statements can check for any boolean expression.

Conclusion

To use a case and switch statement with two variables with JavaScript, we use switch with true.

Categories
JavaScript Answers

How to add a scroll event listener with JavaScript?

Sometimes, we want to add a scroll event listener with JavaScript.

In this article, we’ll look at how to add a scroll event listener with JavaScript.

How to add a scroll event listener with JavaScript?

To add a scroll event listener with JavaScript, we call addEventListener.

For instance, we write

const runOnScroll = (element) => {
  console.log(element);
};

const elements = [...document.querySelectorAll(`...`)];

elements.forEach((element) => {
  window.addEventListener("scroll", () => runOnScroll(element), {
    passive: true,
  });
});

to call querySelectorAll to select all the elements we want to add a scroll listener to.

Then we use the spread operator to spread the entries into an array.

Next, we call elements.forEach with a callback that calls addEventListener to add a scroll listener to each element in elements.

Conclusion

To add a scroll event listener with JavaScript, we call addEventListener.