Verifying the age of a remote JSON file in Javascript without needing to download the full file

I'm working on a Phonegap app that needs to compare a local JSON file with the latest version available remotely. If the remote version is newer, I want to update the local JSON.

To optimize bandwidth usage, I am looking for a way to perform this version check without downloading the entire remote data. Perhaps by just checking the headers...

In my search for a solution, I came across discussions about using the "HTTP ETag header" in threads like this one

Does anyone have suggestions on how to implement something like this using pure Javascript?

Thank you in advance.

Answer №1

When your server responds with a 304 - Not modified status, you have the option to check for an update using the following function:

function checkForUpdate(url, callback)
{
    var request = new XMLHttpRequest();
    request.open('HEAD', url);
    request.onreadystatechange = function() {
        if (this.readyState == this.DONE) {
            if (this.status === 200) {
              // Data is new -- consider making another request to get updated information
            } else if (this.status === 304) {
              // Data has not been modified
            } else {
              // An error occurred while fetching data.
            }
        }
    };
    request.send();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we use vanilla JavaScript to implement an image zoom effect on mouse movement?

I'm attempting to implement an onmousemove event within an image that displays a zoomed image on the right side. Issue: When I move my mouse inside the image, the black box and zoomed image flicker. The zoomed image does not align correctly in the b ...

Determine the position of the anchor/hash in the document

I am really struggling here. Whenever I try to enter index.html#about (or bookmark it and open it), I need to extract the index() of this and place it in the script below as "slideNumber". HTML Structure <div id="slideshow"> <div id="frontp ...

Is there a way to remove a JavaScript Blob file from cache?

There are times when I no longer require a blob along with its "createObjectURL" in my code. How can I ensure that it is completely removed from memory, freeing up space and resources? ...

Error Encountered in Next.js: PDFtron Webviewer - Troubleshooting

I'm currently working on a website that incorporates a dynamically imported PDF viewer within the page. When running the code locally, everything operates smoothly with no errors. However, when executing the "npm run build" command, I encounter the fo ...

Extracting information from a designated website using Python, including pages with search functionality and javascript features

Visit the website where you will find a search input box in html. Input a company name into the search box, select the first suggestion from the drop-down menu (like "Anglo American plc"), navigate to the URL containing information about that specific com ...

Incorporating the namespace attribute during Serialization with Microsoft-Avro-Core

I'm currently converting C# objects into avro file format using the Microsoft-Avro-Core nuget package. The problem I'm encountering is that the json schema definition of the avro schema includes a namespace property, but after serialization, this ...

What is the best way to eliminate the border of an expansion panel in Material-UI?

Is there a way to eliminate the border surrounding the expansion panel in Material-UI? ...

Displaying Angular JS data in the Laravel application using the URL::route facade

I'm currently working on an Angular JS + Laravel Application. On one of the pages, I have a list of users that I fetch, and when a row is clicked, it redirects to the edit user page. Below is a snippet of my code: Blade Template <tbody> &l ...

Retrieving customData from fullCalendar JSON in the dayClick event handler

Apologies for what may be a simple question. I am trying to insert some custom JSON data into the fullCalendar plugin and utilize it within my dayClick callback function. Specifically, I aim to access and modify elements in customData when a day is clicked ...

Solving the conundrum of sending a Javascript date object (formatted datetime) through Laravel API

When my Laravel 5 API fetches datetime columns from my MySQL database, the JSON output is in this format: 2015-08-13 13:45:00 However, the widget that reads this JSON data expects a JavaScript Date Object. How can I convert my datetime to a JavaScript Da ...

Creating an autonomous duplicate of a reversed array using JavaScript

Check out my code snippet here: http://jsfiddle.net/sepoto/Zgu9J/1/ I've started by creating a function that reverses an array: function reverseArray(input) { var reversed = new Array; for(var i = input.length-1; i >= 0; i--) { re ...

Having trouble getting Chrome to load my JavaScript script file

Setting up a server on my computer was a simple task, but now I am facing an issue with loading a script file in my HTML. var express=require('express'); var app=express(); app.listen(3000); app.use(express.static('/scripts' ...

Execute a javascript function using dynamic calling

I have defined several functions inside an array structure as shown below. checkInputType : function(sel){ alert($(sel).prop('type')); }, checkSelect : function(el){ ....... }, ..... These functions can be called like options.checkInput($(thi ...

Angular application making a $http.post request to change the date to a UTC date

I encountered an issue while trying to send data to my REST api that includes a date parameter. During debugging, I noticed that my date parameter is a JS Date object with the correct date in my timezone: Tue Apr 04 2017 00:00:00 GMT+0530 However, once it ...

Merge two arrays based on date and sort them using Angular.js/JavaScript

I am facing a challenge where I have two JSON arrays, each containing a field named date. My goal is to compare the two arrays and merge them into a single array. Check out the code snippet below: var firstArr=[{'name':'Ram','date ...

JavaScript Array Elements Divided into Separate Arrays

I am struggling with a function that is supposed to accept an array as parameters and then log all the elements of the array into a single new array. However, the issue I am facing is that each array element is being printed separately instead of combine ...

Troubleshooting Issue: Failure of Ajax Script to Display Saved Data in Edit Function

Whenever I clicked on the Edit icon in the action column of my data tables, the saved data did not display as expected. I noticed that this issue was only occurring for file input types, while it worked properly for text input types. In the Blade file Ad ...

Updating a single field within a JsonArray and then saving it back to the original file: a comprehensive guide

I am trying to update the date of a specific product object in a JSON file to the current date. However, I am struggling with the logic to achieve this. Can someone assist me in figuring out how to accomplish this task? The JSON file on my local machine c ...

Issue with Thymeleaf form not able to retrieve JSON object

Having trouble retrieving JSON objects instead of strings on my HTML page. Any suggestions? Below is the snippet of HTML code causing the issue: <form th:object="${case}" novalidate="true" data-model="case" action="/hcp/patient/details/insurance" meth ...

Tips for avoiding the use of import statements in Typescript

log.ts contains the code below import {LOG} from './log' LOG.e("tag","error"); LOG.f("tag","error"); LOG.d("tag","error"); I am looking for IntelliSense support in my TS file without affecting the output javascript. I only want this in my Java ...