Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

window.close() not working in IE

Question posted by: snowdonkey (Member) on August 3rd, 2006 07:04 PM
Hello! I'm having difficulty closing a popup window in Internet Explorer.

In the script below playButton and stopButton are images that when clicked, should open a popup window and close it, respectively. audioFile is a URL to an mp3.

Right now the new window will open when playButton is clicked, but will not close when stopButton is clicked. I don't get any error messages. Thanks for your help!

function audioOnOff(audioFile, playButton, stopButton)
{
var audioWindow;

playButton.attachEvent("onclick", playAudio);
stopButton.attachEvent("onclick", stopAudio);

function playAudio()
{
audioWindow = window.open(audioFile, "_blank");
}

function stopAudio()
{
audioWindow.close();
}
}
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Banfa's Avatar
Banfa
The Voice of Reason
4,560 Posts
August 4th, 2006
10:02 AM
#2

Re: window.close() not working in IE
your variable audioWindow is declared in the scope of audioOnOff. That means it only exists for the length of time that function is running so by the time you get to calling stopAudio is is no longer the same instance of the variable and doesn't correspond to the opened window.

I would also not declare playAudio and stopAudio inside audioOnOff so I would declare it this as

Code: ( text )
  1. var audioWindow;
  2.  
  3. function playAudio()
  4. {
  5.         audioWindow = window.open(audioFile, "_blank");
  6. }
  7.  
  8. function stopAudio()
  9. {
  10.        audioWindow.close();
  11. }
  12.  
  13. function audioOnOff(audioFile, playButton, stopButton)
  14. {
  15.        playButton.attachEvent("onclick", playAudio);
  16.        stopButton.attachEvent("onclick", stopAudio);
  17. }



You may also need to do something for the case of the user clicking a play button when a window is already open as you will end up with 2 windows open but audioWindow will only refer to the last window opened.
__________________
A method worth implementing is worth invoking :D

Reply
snowdonkey's Avatar
snowdonkey
Member
37 Posts
August 4th, 2006
04:51 PM
#3

Re: window.close() not working in IE
Ah, makes sense. Very kind for the heads-up on replacing the pop-ups windows. Thank you very much for your help!

Reply
Reply
Not the answer you were looking for? Post your question . . .
169,970 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Javascript / DHTML / Ajax Forum Contributors