We help IT Professionals succeed at work.

window close link

Aleks
Aleks asked
on
258 Views
Last Modified: 2017-04-09
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.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.

Author

Commented:
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

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT

Commented:
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

SnapShot.png
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for the code. That is very helpful.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are welcome.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.