Guide to retrieve the Last-Modified date using Javascript

It is common knowledge that the document.lastModified function returns a string containing the date and time when the current document was last modified.

Is it possible to obtain the Last-Modified for a script?

Here is an example of HTML code:

...
<script id="myapp" type="text/javascript" src="https://test.asl.cloud/cc/js/my-app.js"></script>
...
<button onclick="myFunction()">Try it</button>
<p id="test_doc"></p>
<p id="test_js"></p>
...

This is the Javascript code involved:

myFunction = function() {
 var x = document.lastModified;
 document.getElementById("test_doc").innerHTML = x;

 var se = document.getElementsByTagName('script');
 var s;
 for (var i = 0; i < se.length; i++) {
  if (typeof se[i].src !== 'undefined' && se[i].src.match('cloud')) {
   s = se[i];
   break;
  }
 }
 console.log(s);
 x = s.lastModified;
 document.getElementById("test_js").innerHTML = x;
}

If you want to see this code in action, check out this JSFiddle link.

Answer №1

If you're looking to find out the last modified date of a script, one method is to make an AJAX request for the script and then check the Last-Modified header.

var client = new XMLHttpRequest();
client.open("HEAD", "myscript.js", true);
client.onreadystatechange = function() {
  if(this.readyState == 2) {
    console.log(client.getResponseHeader("Last-Modified"));
  }
}
client.send();

As an example, using

https://code.jquery.com/jquery-3.0.0.js
in place of myscript.js would output:

Thu, 09 Jun 2016 18:32:50 GMT

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

Creating an accordion feature within an ng-repeat using AngularJS based on user clicks

When I click on the span with ng-click="click_subcat(opp.ct_nm);, the content is loaded within the <span ng-click="click_pdms(opp.sbct_nm);" style="color:white;cursor:pointer">{{opp.sbct_nm}}</span>. The issue arises when I click on that span a ...

How can I use jQuery to automatically redirect to a different HTML page after updating a dynamic label?

When I click on a button in first.html, a function is called to update labels in second.html. However, despite being able to see second.html, I am unable to see the updated values in the labels. Can someone please advise me on how to achieve this? functi ...

Querying the Collection for document counts is failing to display any data

Hey there, I am currently in the process of developing a social media app with the MEAN stack. One of the key features I'm working on is the ability to list the users that a person is following. However, I have encountered an issue as Collections.Find ...

Issue with date range filter functionality not functioning as expected

Struggling to get the date range filter to function properly. Selecting a date triggers it, but nothing is being added to the var filter. I've spent hours trying to solve this issue with no progress. html <div class="form-group"> <input ...

How can you retrieve input values from a form using a button click but without requiring a

I am facing an issue with the delete button on my form. The form has three buttons - one to edit the form on another page, one to add a value on the existing page, and one to delete a value. While the submit and edit buttons work fine, I am struggling wi ...

Retain the jQuery dropdown menu in an open state while navigating to a different webpage on the

I am encountering an issue with a drop-down menu on my website. Whenever I click on a submenu link, the new page opens but the menu automatically closes. However, I want the active menu to remain open even on the new page. To solve this problem, I believe ...

The following MongoDB errors unexpectedly popped up: MongoNetworkError: connect ETIMEDOUT and MongoServerSelectionError: connect ETIMEDOUT

I've been working on a React and NextJS App for about a month now, utilizing MongoDB as my database through MongoDB Atlas. I'm currently using the free version of MongoDB Atlas. For the backend, I rely on NextJS's api folder. Everything wa ...

JavaScript: The function is encountering issues with properly formatting the numbers

I am currently facing an issue with a function I have created to format numbers for currency by inserting commas every 4 digits. The problem lies in the fact that the first 4 numbers do not have a comma added where it should be, and the formatting only wor ...

Creating an external link in Angular with query parameters

I have created an app where users have their addresses listed, and I want to implement a feature that allows me to open Google Maps when clicking on the address. However, I am currently facing an issue where instead of getting the actual value of {{ this. ...

React Hook Form: When Submitting, Transform Date Object into String Date within an Array

Below is the data I am working with: { status: "Reserved", label: "Note", title: "Login Fragment - Navigation component With Coroutine", shareWith: "", notification_method: "Every Morning", notification_method_specific_time: Sat ...

"Enhancing User Experience with AngularJS by Dynamically Modifying and Refresh

I'm currently attempting to dynamically add HTML elements using JavaScript with a directive: document.getElementsByClassName("day-grid")[0].innerHTML = "<div ng-uc-day-event></div>"; or var ele = document.createElement("div"); ele.setAttr ...

provide an element reference as an argument to a directive

I am trying to figure out how to pass an element reference to a directive. I know that I can get the reference of the element where the directive is applied using private _elemRef: ElementRef but my goal is to pass the reference of another element to the ...

What is the best way to link buttons to specific drop down sections?

How can I create multiple drop down divs with different content for each button click? JSFiddle: http://jsfiddle.net/maddiwu/xe6xtfqh/ .slide { overflow-y: hidden; max-width: 100%; overflow-x: hidden; max-height: 100vw; /* approximate ...

What steps can be taken to customize this code in order to develop a dictation application?

Currently, I have a code that functions by comparing two strings: str2 (which represents user input) and str1 (our reference string). The purpose is to determine whether any words in str2 are spelled correctly or incorrectly. So far, the code works well. H ...

Maintain dropdown menu visibility while navigating

I'm having an issue with my dropdown menu. It keeps sliding up when I try to navigate under the sub menu. I've spent all day trying to fix it, testing out various examples from the internet but so far, no luck. Any help would be greatly apprecia ...

Difficulties encountered when trying to update the registry using ajax in Rails

https://i.sstatic.net/Udb9L.png When I update a registry, the updated record row becomes disorganized. Here is my code snippet: index.html.erb <table id="enterprises-items"> <thead> <tr> <td>Nombre</td> &l ...

"Jquery's .append function may sometimes display as undefined when

$("#clckjson").click(function() { $(document).ready(function() { $.getJSON("fuelj.json", function(data) { $(data).each(function(i, item) { console.log(item.stationID); var $table = $('<table>'); $table.a ...

The functionality of ng-switch appears to be malfunctioning

Take a look at this code snippet: <script src="angular.min.js"></script> <script src="jquery-1.6.4.js"></script> <script type="text/javascript"> var app = angular.module('myApp', []); app.controller("myContr ...

Range slider position not being updated after user interaction

I am working on a webpage with an embedded video and a range slider that serves as a progress bar. Using Javascript, I update the value of the range slider as the video plays, allowing users to navigate through the video content. However, I have encounter ...

Currently trapped within the confines of a Next.js 13 application directory, grappling with the implementation of a

I need to figure out how to export a variable from one component to layout.tsx in such a way that it is not exported as a function, which is currently causing the conditional check in the class name to always be true. Below is the code snippet: // File w ...