Question

I use the MediaElement.js framework to build my audio players... But, is a podcast and is normal see users closing the window accidentally.

I'm searching for scripts to this, but absolutely nothing works.

How alert the user when he close the window with the player working...?

If is playing: ask if the user really want this. If the player isn't playing: close the window..

Was it helpful?

Solution

Add an event as follows:

$(window).bind("beforeunload", function(evt) {//gets called before the user leaves the page
    //check if audio is already running
    if(isPlayingAudio()) {
        //shows a confirm message asking user if they want to stay
        return "Are you sure you want to leave while playing music?";
    }
})

If your elements are on the page and not stored in javascript

function isPlayingAudio() {
    var $playing = $('audio').filter(function(i, $audio) {//all audio not paused
        return !$audio.prop('paused');
    });
    return $playing.length === 0;//no audio elements are playing...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top