How to stop a youtube iframe video after closing a popup

If you ever embed a youtube video in a popup window, unless you use a third-party modal plugin, you would need to stop the video when the popup is closed so that it does not keep playing in the background. To do this I found the code below on StackOverflow by shabeer90.