Search code examples
javascriptjqueryhtmlevents

How to pause a video when focus leaves my web page (html5)


I want to be able to pause a video when the user clicks on an href that links to a third party web site and start it again when focus returns. I have searched for events but am unable to find one that works eg onunload onchange. I have an event handler that starts a new video when one stops and scrolls down the page (javascript) but I am stuck on this problem. I tried an href that called a javascript function but it became messy (the href is generated dynamically).


Solution

  • window.addEventListener('focus', function() {
      document.title = 'focused';
    });
    
    window.addEventListener('blur', function() {
      document.title = 'not focused';
    });
    

    You can use this code to get focus and blur event for window tab and call your play and pause functions from here.