views:

30

answers:

2

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?

+2  A: 

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 >
nerkn
And at the end of the JS, the code to remove the loadingindicator.
Konerak
I think this is the only way to go - as far as I know, there is no way to find out whether a resource is cached or not
Pekka
Yes, this is what I'm doing today. It does not detect whether the file was loaded from cache, only that the code is loaded.
Joernsn
+1  A: 

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?

Joernsn
Interesting idea! This might work.
Pekka