Update data on a page without refreshing

2021/01/30 23:41 · jquery ·  · 0评论

I have a website where I need to update a status.
Like for a flight, you are departing, cruise or landed.
I want to be able to refresh the status without having my viewers to have and reload the whole page. I know there is a way to do it with AJAX and jQuery, but I don't have any understanding of how that works. I also don't want them to have and click a button.
If anybody knows how that would be done I much appreciate it!

In general, if you don't know how something works, look for an example which you can learn from.

For this problem, consider this DEMO

You can see loading content with AJAX is very easily accomplished with jQuery:

    // don't cache ajax or content won't be fresh
    $.ajaxSetup ({
        cache: false
    var ajax_load = "<img src='http://automobiles.honda.com/images/current-offers/small-loading.gif' alt='loading...' />";

    // load() functions
    var loadUrl = "http://fiddle.jshell.net/deborah/pkmvD/show/";

// end  

Try to understand how this works and then try replicating it. Good luck.

You can find the corresponding tutorial HERE


Right now the following event starts the ajax load function:


You can also do this periodically: How to fire AJAX request Periodically?

(function worker() {
    url: 'ajax/test.html', 
    success: function(data) {
    complete: function() {
      // Schedule the next request when the current one's complete
      setTimeout(worker, 5000);

I made a demo of this implementation for you HERE. In this demo, every 2 seconds (setTimeout(worker, 2000);) the content is updated.

You can also just load the data immediately:


Which has THIS corresponding demo.

Suppose you want to display some live feed content (say livefeed.txt) on you web page without any page refresh then the following simplified example is for you.

In the below html file, the live data gets updated on the div element of id "liveData"


<!DOCTYPE html>
    <title>Live Update</title>
    <meta charset="UTF-8">
    <script type="text/javascript" src="autoUpdate.js"></script>
<div id="liveData">
    <p>Loading Data...</p>

Below autoUpdate.js reads the live data using XMLHttpRequest object and updates the html div element on every 1 second. I have given comments on most part of the code for better understanding.


window.addEventListener('load', function()
    var xhr = null;

    getXmlHttpRequestObject = function()
            // Create a new XMLHttpRequest object 
            xhr = new XMLHttpRequest();
        return xhr;

    updateLiveData = function()
        var now = new Date();
        // Date string is appended as a query with live data 
        // for not to use the cached version 
        var url = 'livefeed.txt?' + now.getTime();
        xhr = getXmlHttpRequestObject();
        xhr.onreadystatechange = evenHandler;
        // asynchronous requests
        xhr.open("GET", url, true);
        // Send the request over the network


    function evenHandler()
        // Check response is ready or not
        if(xhr.readyState == 4 && xhr.status == 200)
            dataDiv = document.getElementById('liveData');
            // Set current data text
            dataDiv.innerHTML = xhr.responseText;
            // Update the live data every 1 sec
            setTimeout(updateLiveData(), 1000);

For testing purpose: Just write some thing in the livefeed.txt - You will get updated the same in index.html without any refresh.



Note: You need to run the above code on the web server (ex: http://localhost:1234/index.html) not as a client html file (ex: file:///C:/index.html).

You can read about jQuery Ajax from official jQuery Site:

If you don't want to use any click event then you can set timer for periodically update.

Below code may be help you just example.

function update() {
  $.get("response.php", function(data) {
    window.setTimeout(update, 10000);

Above function will call after every 10 seconds and get content from response.php and update in #some_div.

I think you would like to learn ajax first, try this: Ajax Tutorial

If you want to know how ajax works, it is not a good way to use jQuery directly. I support to learn the native way to send a ajax request to the server, see something about XMLHttpRequest:

var xhr = new XMLHttpReuqest();
xhr.open("GET", "http://some.com");

xhr.onreadystatechange = handler; // do something here...
文章标签: ,   ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!