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.
ASPJavaScriptWeb Development

Avatar of undefined
Last Comment
Julian Hansen
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
Aleks
Flag of United States of America image

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

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Aleks
Aleks
Flag of United States of America image

ASKER

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

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo