0

I have old file that has been changed. One of the requests for this file/project modification is when user clicks OK to submit the form and close the current browser window. I'm wondering if this can be achieved with plain/vanilla JavaScript? So I have form and button:

<form name="test" id="test" action="/save_form.cfm">
   <input type="submit" value="OK">
</form>
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

5 Answers5

0

You can simply use:

window.close();

You can type it in your input tag or make a new script tag with that code in it, whenever a button is clicked.

For example

<input type="submit" value="OK" onclick="closeWindow();">

In addition, you can add return false to 'onclick' to prevent the default actions.

FloWy
  • 934
  • 6
  • 14
0

Yes, JavaScript can close the current window, but bare in mind that if the browser has other tabs open, the browser won't close.

var closeBtn = document.querySelector("#test input");

closeBtn.addEventListener("click", ()=>{
    window.close()
})
Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35
0

you can use onSubmit event as shown here to call a function and then when its done use window.close() to close the window.

mog13
  • 81
  • 3
0
window.close();

However you can't just close any window. It only works if it's one that you opened yourself. (security feature).

This was already answered in detail here:

0

Assuming the window was opened using window.open() , have your save-form.cfm return a basic html page with script tag to close window when submit and validation is succesfaul

<html>

<body>
  <script>window.close()</script>
  <p>Window should close automatically</p>
</body>

</html>
charlietfl
  • 170,828
  • 13
  • 121
  • 150