Is there a way to use JavaScript to retrieve a list of files that were loaded by the browser during the last request

I'm just starting out in the world of JavaScript and I have a query: is there a method to retrieve a list of files that were loaded by the browser during the last request? To clarify, when a browser loads a webpage, it not only downloads the HTML file but also any CSS, JPEG, and other necessary files. I am interested in knowing through a JavaScript script on the page the names of those loaded files. Thank you in advance!

Answer №1

If you're looking for a solution using jQuery, here's one approach you could take:

$('script').each(function(){
    var file = $(this).attr("src");
    $("#result").append(file + "<br />"); 
});

$('link').each(function(){
    var file = $(this).attr("href");
    $("#result").append(file + "<br />"); 
});

$('img').each(function(){
    var file = $(this).attr("src");
    $("#result").append(file + "<br />"); 
});

Check out the demonstration on this JSFiddle link.

If you prefer a pure JavaScript solution, you can try something like this:

var script = document.getElementsByTagName("SCRIPT");
for (var i = 0; i < script.length; i++) {
    document.getElementById("result").innerHTML += script[i].src + "<br />";
}

var image = document.getElementsByTagName("IMG");
for (var i = 0; i < image.length; i++) {
    document.getElementById("result").innerHTML += image[i].src + "<br />";
}

var link = document.getElementsByTagName("LINK");
for (var i = 0; i < link.length; i++) {
    document.getElementById("result").innerHTML += link[i].href + "<br />";
}

Here is the JSFiddle showcasing the implementation with pure JavaScript: http://jsfiddle.net/Lotc8dda/

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

What is causing the error message "Uncaught TypeError: Cannot read property 'helpers' of undefined" to occur in my straight-forward Meteor application?

As I follow along with the tutorial here, I encounter an issue after replacing the default markup/code with the provided HTML and JavaScript. The error message "Uncaught TypeError: Cannot read property 'helpers' of undefined" appears. Below is t ...

The data from the server is inaccessible to node.js

I am a beginner in nodejs and jquery, trying to retrieve data from a server and use it to update a webpage: Using jquery on the client side: I would like to change the text inside '#info' element with the data fetched from the server. $('# ...

encountering difficulties with installing dependencies using yarn or npm

After cloning a repository, I attempted to install the dependencies using npm install or yarn but encountered the following errors: For Yarn: https://gyazo.com/2fdf52c4956df2e565cc0b1cedf24628 For npm install: https://gyazo.com/a1d197e9ead89dbe4a7d3c5b8f ...

The request for an advertisement was successful, however, no ad could be displayed as there was insufficient ad inventory available. Please handle the situation appropriately with the

Using react-native, I am trying to incorporate ads into my app but encountering an error. Despite attempting various solutions, nothing seems to work. It appears that the issue may lie with the AdMob Android SDK. While I have reviewed SDK videos related to ...

CSS transitions following with visual/codepen

Does anyone know how to animate two images, bringing them to a central point before rotating them simultaneously? I'm struggling with transforming the svgs prior to starting the animation. Take a look at this example. My goal is to apply transform: r ...

Customizable form with dynamic content and interactive button within a set location

One distinct challenge not addressed in the similar inquiries below relates to a FORM component with both a variable segment and a fixed footer for submit buttons. The objective is to present: A header (a table set to 100% width including a logo and tex ...

Updating a property in an object within an Angular service and accessing it in a different controller

I am currently utilizing a service to transfer variables between two controllers. However, I am encountering difficulties in modifying the value of an object property. My goal is to update this value in the first controller and then access the new value in ...

Selenium in Java is unable to send keys or click on elements

I am attempting to login to this specific website Below is the code I am using : driver.findElement(By.id("userid_sebenarnya")).sendKeys("myUserName"); This is the property for the input text field with id='userid_sebenarnya' <div class="f ...

Implementing a dynamic top navbar that transitions to the side on desktop with Bootstrap

I have been faced with the challenge of creating a responsive navigation bar that appears at the top on mobile devices and on the side on desktops. The issue arises when considering how to structure the rows and columns to achieve this. For mobile views, ...

Passing data retrieved from fetch requests in ReactJS using context: Best practices

Just started learning React and need some help. I'm trying to pass variables with json data to a component for further use but running into errors. What changes should I make to use variables with json data from Store.js in the product.js component? T ...

Encountered a problem during the installation of tensorflowjs for node | Received an error: Command failed: node-pre-gyp install

While attempting to install @tensorflow/tfjs-node, I encountered the following error: Command failed: node-pre-gyp install --fallback-to-build. Is there a solution to resolve this issue? Below is the complete log: npm ERR! code 1 npm ERR! path E:\ ...

How come my variable doesn't show up in the view even though I filled it in the controller?

I'm having trouble with my AngularJS setup. The angular.min.js file is located in the js folder, following a code example from O'Reilly's book. However, for some reason it is not working on my computer. //controller.js function HelloCont ...

Enhance your bootstrap accordion by incorporating stylish up and down arrows using the <accordion> element

I am currently developing a sophisticated web application using Angular and TypeScript, and I have decided to incorporate accordions in some sections. The setup for these accordions involves TypeScript/Bootstrap as shown below: <accordion> <acco ...

The Oscillator node in the Web Audio API is unable to be started more than once due to an error

Every time I attempt to start, stop, and then start my oscillator again, I encounter the following error message: Uncaught InvalidStateError: Failed to execute 'start' on 'OscillatorNode': cannot call start more than once. Although usi ...

What is the best way to create and deliver favicons using Webpack?

Currently, I am working on a universal React project and I need to serve favicons in an Html.js component that is rendered server-side. I came across this package which automatically generates over 30 different sizes of favicons, and I am interested in us ...

CSS - Struggling to identify the source of unwanted horizontal scrolling?

Recently, I've been attempting to make adjustments to a responsive website that utilizes media queries. Despite my efforts, whenever the site is viewed on a mobile-sized screen, it consistently requires horizontal scrolling regardless of the screen&a ...

CSS: Firefox shows the menu with adequate right margin

Just delving into CSS and have successfully crafted a dropdown menu. It's all smooth sailing in Chrome and IE, but Firefox presents two challenges: 1) The color gradient appears darker in Firefox compared to the lighter green in Chrome and IE. 2) An ...

Contrast between Q.defer() and the Promise() function

I am puzzled by the differing behavior of the following code when using Q.defer() as opposed to Promise() Scenario 1 : When Q.defer() is used getDocument(id) .then(function (response) { console.log('in first then') return 'from tw ...

Can you provide guidance on how to specifically specify the type for the generics in this TypeScript function?

I've been diving into TypeScript and experimenting with mapped types to create a function that restricts users from extracting values off an object unless the keys exist. Take a look at the code below: const obj = { a: 1, b: 2, c: 3 } fun ...

Extract the URL parameter and eliminate all characters following the final forward slash

Here is the URL of my website: example http://mypage.com/en/?site=main. Following this is a combination of JavaScript and PHP code that parses the data on the site. I am now looking for a piece of code that can dynamically change the URL displayed in the ...