Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

window close link

This may be silly but for some reason my code is not working.
I am trying to close a window with a link (on the same page)

 <a href="javascript:window.close()" class="btn btn-primary m-t">Close window</a>

Open in new window


When I click on it nothing happens. I am using Firefox.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

You cannot close a window that your script did not open. You have to have issued a window.open() on the page for that script to work i.e. you can't put script inside a page to close itself.
Avatar of Aleks

ASKER

I actually used another page script to open the pop up.

<a href="javascript:;" onClick="MyPopUpWin('<%=(rs_apis.Fields.Item("service_url").Value)%><%=(rs_apis.Fields.Item("api_return_url").Value)%>',1000, 800)" " class="btn btn-primary">Create key</a>

Open in new window

also check some of the explanations here:

Window.close()
https://developer.mozilla.org/en-US/docs/Web/API/Window/close

in your FF's setting, make sure option: dom.allow_scripts_to_close_windows is set as true.

got to about:config page, set the option: dom.allow_scripts_to_close_windows to true

User generated image
Take a look at this sample code
HTML (Main Window)
<a href="#" onClick="return MyPopUpWin('t2281-popup.html')" class="btn btn-primary">Create Popup</a>
<a href="#" onClick="newwin.close();this.style.display = 'none'" class="btn btn-danger closePopup">Close</a>

Open in new window

JavaScript
<script>
var newwin;
function MyPopUpWin(url) {
  newwin = window.open(url, 'MyPopup', "width=300px,height=100px,scroll=no");
  document.querySelector('.closePopup').style.display = 'inline';
  return false;
}
</script>

Open in new window

Popup Window
<!doctype html>
<html>
<body>
I am the popup
<a href="#" onClick="window.close()">Close</a>
</body>
</html>

Open in new window

Working sample here
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

Thanks for the code. That is very helpful.
You are welcome.