Joernsn
Joernsn

Reputation: 2349

JavaScript: How can I check whether a file is cached?

My site loads a pretty large js file the first time a user visits, and I want to write something like "Loading .. for the first time" it the file isn't from cache.

Is this possible in javascript?

Upvotes: 4

Views: 2472

Answers (2)

Joernsn
Joernsn

Reputation: 2349

What about including a dynamic timestamp at the end, and checking it afterwards?

var t=<?php echo time(); ?>;

At least i'd get an indication after the fact. Or would I mess up other caching mechanisms by updating the file?

Upvotes: 2

nerkn
nerkn

Reputation: 1980

in your js

var loadedMyJS = true

in your html

 <script >
 function loadingIndicator(){ 
   document.getElementById('loadingDiv').style.display=''; //to hideit will be 'none' 
 }
 if(typeof(loadedMyJS) == 'undefined'){
    loadingIndicator();
 }

 </script >

Upvotes: 2

Related Questions