Connecting Tech Pros Worldwide Help | Site Map

window.close() not working in IE

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 3rd, 2006, 07:04 PM
snowdonkey's Avatar
Member
 
Join Date: Aug 2006
Location: Chicago
Posts: 37
Default window.close() not working in IE

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();
}
}
Reply
  #2  
Old August 4th, 2006, 10:02 AM
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 5,731
Default

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

Expand|Select|Wrap|Line Numbers
  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. }
  18.  

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.
Reply
  #3  
Old August 4th, 2006, 04:51 PM
snowdonkey's Avatar
Member
 
Join Date: Aug 2006
Location: Chicago
Posts: 37
Default

Ah, makes sense. Very kind for the heads-up on replacing the pop-ups windows. Thank you very much for your help!
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.