2

I am writing a blog, and I want there to be a like button, but I am curious, how can I get the button to update for everybody?

<button onclick="like()">Like</button>
<span id="likecount">0</span>

and js

function like(){
  document.getElementById('likecount').innerText++;
  //Toggle code here
}
0xdw
  • 3,755
  • 2
  • 25
  • 40
  • Hi, perhaps use a database so that the count is stored? this might be of interest https://stackoverflow.com/questions/14597922/how-to-create-your-own-like-button-not-facebook-related – IronMan Nov 07 '20 at 01:43

1 Answers1

1

If you mean to update the like just like FB/Twitter/etc do, you'll need a connection that allows the server to send updates to connected clients, without the clients requesting anything. This can be done using WebSockets / Socket.io, but needs a different infrastructure than standard blogs.

An alternative would be to save the like to a database, and polling the like data every few seconds. It's less fancy, but also less complex.

Emre Koc
  • 1,481
  • 10
  • 18