Passa al contingut principal

Don’t Stop The Music: Links on Pages Playing Audio

Over at ShopTalk, we've gotten a number of requests from folks that wished links in our show notes would open in a new tab. They've gotten burned by clicking a link on the page while listening to a podcast via our embedded player. When the browser leaves our page and goes to the link, the audio stops playing. We can do something about that.


One option would be to literally apply a target attribute to each link, so that those links open in a new window or tab.



<a href="#" target="_blank">Show Note Link</a>

We would have to go through our whole archive of shows to add that right to the HTML. Not un-doable, but not great and also not very flexible.


Personally I don't generally like new tab links and I think that's the general UX consensus as well. Don't mess with the default unless you have a good reason. We do have a good reason here, but if we think a bit more about it, what we're actually trying to do here is prevent the sound from abruptly stopping. So let's only alter that behavior when the audio is actually playing.


We're using MediaElements.js for the player and also jQuery on the page. This player provides events we can bind to for starting and stopping of audio.



var player = $('audio').mediaelementplayer();

$(player).on("play", function(event) {

// Links in new tabs
$("a").attr("target", "_blank");

});


Cool. You could get fancier with that, by removing the attribute on pause. I'll let you decide how you want to handle it.


But what if you hate changing default behavior like this? That's understandable. There is another way we could do this, and that's using the onbeforeunload event (fires when the browser is about to change pages) to confirm the navigation.



It's plenty easy. We just need to remove it when the audio isn't playing.



var player = $('audio').mediaelementplayer();

$(player).on("play", function(event) {

window.onbeforeunload = function() {
return "The podcast will stop playing if you navigate away.";
}

}).on("pause", function(event) {

window.onbeforeunload = void(0);

});


Both ways work. I went with the target="_blank" technique for ShopTalk, but CodePen uses onbeforeunload quite a bit when you have unsaved changes to work, to prevent you from losing it. Also related: in the past we've bent over backwards making a whole site Ajax to prevent audio from stopping as you navigated around.


Don’t Stop The Music: Links on Pages Playing Audio is a post from CSS-Tricks






via CSS-Tricks http://css-tricks.com/dont-stop-the-audio/

Comentaris

Entrades populars d'aquest blog

10 alternativas a Cuevana para ver películas online

10 alternativas a Cuevana para ver películas online : Durante este último tiempo, en Cuevana se sucedieron varios “problemas” por los cuales hubo que ajustar algunas cosas antes de tiempo (como el rediseño del sitio), que dejaron a algunos usuarios ciertos problemas para acceder a las películas o series del portal. Pero realmente esto es algo que no incumbe a los usuarios y, como sabemos, existen muchas otras alternativas a Cuevana dando vueltas por Internet, que intentaremos presentar aquí mismo. Los sitios que repasaremos funcionan del mismo modo que Cuevana, mediante la instalación de un plugin que permite visualizar los videos de Megaupload o WUShare, entre otros servicios, en una calidad de imágen realmente excelente. Tal como sucede con el más popular servicio, todos ellos tienen publicidad que en algunos casos resulta insoportable, pero como dice Federico en DotPod “a caballo regalado no se le miran los dientes”. Alternativas a Cuevana 1. Moviezet Posiblemente el mejor clon d

Sitio alternativo a Cuevana: Moviezet

Sitio alternativo a Cuevana: Moviezet : Nadie se quiere enfrentar al monstruo Cuevana , tan popular por estos días que es casi imposible ver tu serie favorita o tu película sin tener problema de saturación de tráfico. Pero hay proyectos muy sanos y prometedores, sobre todo porque están basados como una muy buena alternativa . Señores estamos hablando obviamente de un sitio alternativo a Cuevana, llamado Moviezet. Como bien dijimos, Moviezet es una excelente alternativa a Cuevana, ya que podremos ver películas y series de forma gratuita sin necesidad de que existan cortes – al mejor estilo Megavideo – y que podremos tener un seguimiento, es decir, si miramos una serie, podremos ver toda la lista con los capítulos disponibles. Lo que tiene de novedoso este sitio web Moviezet , es que tiene películas y series que quizá en Cuevana no se puedan conseguir, pero atención, que puede suceder lo mismo, pero al revés. Entonces aquí intervenimos nosotros y te daremos un sabio consejo, para no

Learn Composition from the Photography of Henri Cartier-Bresson

“Do you see it?” This question is a photographic mantra. Myron Barnstone , my mentor, repeats this question every day with the hopes that we do “see it.” This obvious question reminds me that even though I have seen Cartier-Bresson’s prints and read his books, there are major parts of his work which remain hidden from public view. Beneath the surface of perfectly timed snap shots is a design sensibility that is rarely challenged by contemporary photographers. Henri Cartier-Bresson. © Martine Franck Words To Know 1:1.5 Ratio: The 35mm negative measures 36mm x 24mm. Mathematically it can be reduced to a 3:2 ratio. Reduced even further it will be referred to as the 1:1.5 Ratio or the 1.5 Rectangle. Eyes: The frame of an image is created by two vertical lines and two horizontal lines. The intersection of these lines is called an eye. The four corners of a negative can be called the “eyes.” This is extremely important because the diagonals connecting these lines will form the breakdown