In my project I need to load a very big .js file from javascript and cache it.
I wanted to load the script file from the login page, and then in all other page to put the script in normal tag like this <script src="assets/js/app.js"></script>
The fact is that, I tried 3 different methods, but in each one the file is not cached and if I change page, from login to another, the browser download it again.
In the first method i created an element with javascript and then i added this element to my page:
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Fire the loading
head.appendChild(script);
In the second, I used jQuery's getScript:
$.getScript('assets/js/app.js',function(){
alert('OK');
});
And in the third i tried with an ajax call:
$.ajax({
type: "GET",
url: "assets/js/app.js",
success: function(){},
dataType: "script",
cache: true
});
But no one of these methods seems to caching the scripts. Is there a method to obtain that?
UPDATE
I solved the problem. The first method worked. I forgot that I had the "disable cache" checked in the chrome's debug settings. (I tried with IE11, Firefox, Edge and Chrome)
htmldocuments?, which may not be already loaded?