-1

I created a HTML file and made four sections in it, in each section I added a cross button to close tab using following code

function exitpage () {
    let new_window = open(location, '_self');
    
    // Close this window
    new_window.close();
    return false;
}

By adding onclick to that cross button, this code is working on 1st and 2nd section as when clicked cross button tab is closed. But this code is not working on 3rd and 4th section as code is as it is same.

AGE
  • 3,604
  • 3
  • 35
  • 58
  • Please add the relevant HTML to compliment your question – AGE Sep 09 '21 at 16:15
  • Also, you need to use `window.open` otherwise... what is the function `open` ? Of course `close` will not work if `new_window` does not have such a method – AGE Sep 09 '21 at 16:16
  • I doubt `window.close` is not working. It is more likely your code is not getting called because of an issue with your markup, e.g. you have an unmatched tag or something like that. I suggest adding an `alert` to the top of your function to see if it is even getting called. Or better yet, learn to use [F12 developer tools](https://stackoverflow.com/questions/66420/how-do-you-launch-the-javascript-debugger-in-google-chrome). – John Wu Sep 09 '21 at 16:17
  • Welcome to Stack Overflow! Please update the question to include a [mcve] which demonstrates the problem. You can even create a runnable example using a stack snippet. In that example, please indicate specifically what isn't working as expected. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Sep 09 '21 at 16:19

1 Answers1

1

Window.close() method "can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script." - MDN (link)

Bingo213
  • 9
  • 2