Reputation: 297
What's the best way to constantly receive updates for few <div>
's without refreshing web page?
Is the Server sent events best option?
Upvotes: 1
Views: 100
Reputation: 144
If you are just trying to refresh the contents of a DIV once in a while without reloading the entire page, I highly recommend just using a JavaScript framework called JQuery. It has very easy-to-write code, and it's very simple to set a timer and reload a div at a certain interval - or on a click.
All you need to do is include the script tag
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
in your document, and JavaScript can then use all sorts of AJAX shorthand and DIV loading code, such as $("#divID").load("http://somesite.com/new/content/for/div");
Upvotes: 1
Reputation: 19909
Websockets (push technology) is your answer. Repeated ajax calls will result in a lot of unnecessary requests. Have a look at this question.
Upvotes: 1
Reputation: 1147
Server Sent Events are not supported in all browsers.
Maybe take a look at websockets
Upvotes: 0
Reputation: 626
If you only need to get a couple updates, old school ajax short polling is probably fine. If you need to maintain a constant connection that updates at random intervals, you'll need a more robust option, to which there's a lot.
socket.io, nodejs, faye, comet, websockets, etc. Depends on the compatibility level you want with browsers and your exact needs.
EDIT: Just to update a couple DIVs you're probably fine just doing a few AJAX requests.
Upvotes: 0