0

I have a simple function, which should close the tab if a user clicks a button. For some reason, I keep getting an error that it is not a function.

It's a simple function, and I have no idea why it wouldn't work...

Uncaught TypeError: deny is not a function at HTMLButtonElement.onclick (login.html:183)

Any help would be welcome

EDIT: Thanks for pointing to other threads. It might be true that it wouldn't close the window. BUT why does it say that deny is not a function?

function deny(){
  console.log("deny function executed.. :D ");
 window.close();
};
<button id="deny" onclick="deny()">
 <span id="d">Close</span>
</button>
DrDoom
  • 325
  • 1
  • 2
  • 12
  • What is not a function? `deny()`? Please add the exact error message (and preferable a [mcve]) – Andreas Mar 09 '20 at 11:02
  • Does this answer your question: https://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome – Akash Bhardwaj Mar 09 '20 at 11:07
  • Does this answer your question? [How to close current tab in a browser window?](https://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window) – Mohammad Javidi Mar 09 '20 at 11:09
  • where is the error? The code runs successfully and prints log message. @Droom – Jeni Mar 09 '20 at 11:29
  • when I click the "deny" button, I get a console log error deny is not a function. Thats why I'm baffled... it's a simple code. it should execute without a problem – DrDoom Mar 09 '20 at 11:36
  • The function has to be defined before the DOM node. Move it into the `` – Andreas Mar 09 '20 at 12:39

2 Answers2

1

window.close() will not work on windows that weren't opened with the window.open() method

van
  • 550
  • 4
  • 15
1

Scripts may close only the windows that were opened by it. So you cannot close the current window by window.close() method.

Please refer this link. window.close and self.close do not close the window in Chrome

Jeni
  • 320
  • 2
  • 12