Connecting Tech Pros Worldwide Help | Site Map

window.close() not working in IE

  #1  
Old August 3rd, 2006, 08:04 PM
snowdonkey's Avatar
Member
 
Join Date: Aug 2006
Location: Chicago
Posts: 37
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();
}
}
  #2  
Old August 4th, 2006, 11:02 AM
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,117
Provided Answers: 6

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

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
offsetHeight and offsetWidth not working in IE and Mozilla both dmjpro answers 4 March 4th, 2009 04:25 AM
window.open not working in IE when trying to write 10,000+ characters karamorf answers 7 July 25th, 2007 11:45 PM
close() not working in Mac IE 5.2 Alan Little answers 2 July 23rd, 2005 02:24 PM