3

I'm making an audio heavy page. I'm using the JavaScript audio element. The code looks like this:

JavaScript:

var x = new Audio("x.mp3");
var y = new Audio("y.mp3");
var z = new Audio("z.mp3");

function playMe(n){
    n.play();
}

HTML:

<button class="button" onclick="playMe(x)")>Play</button>
<button class="button" onclick="playMe(y)")>Play</button>
<button class="button" onclick="playMe(z)")>Play</button>

I'm trying to find a way to save bandwith, and I was suggested that I should use either cache headers in the web server or a cache manifest. Here is the thread:

How can I reuse the audio a browser already downloaded so it doesn't download it again in other pages that use the same audio file?

Is there a way in JavaScript (or any other way) to check the contets of the browser's cache? The goal is to achieve something like this:

if (file in the cache){
    use the file in the cache;
}
else{
    var x = new Audio("x.mp3");
    var y = new Audio("y.mp3");
    var z = new Audio("z.mp3");
}

The audio file is downloaded when the Audio object is declared.

Is there a way to do this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
NPN328
  • 1,863
  • 3
  • 27
  • 47
  • have a look at this thread http://stackoverflow.com/questions/5313646/how-to-preload-a-sound-in-javascript – piatek Sep 23 '12 at 11:18
  • 3
    That's the browser's job, not yours. Just make sure the server sends the correct headers. – Niko Sep 23 '12 at 11:20

0 Answers0