0

Is it possible to close a page that was opened in new tab? I tried below codes but didn't work.

function closeCurPage()
{
   window.close();
}

I called the above javascript function on button click like below:

<asp:Button ID="Button1" runat="server" Text="Close" OnClientClick="closeCurPage();"/>
J-J
  • 1,063
  • 5
  • 22
  • 47
  • so you want to close the child page or parent page? I mean the new page which was opened or the page from which you opened that new page? – Sudhansu Choudhary Jul 09 '15 at 07:57
  • It is just a page opened in new tab sir, that's what I want to close sir – J-J Jul 09 '15 at 07:59
  • Is this button on same page which you want to close? – Mairaj Ahmad Jul 09 '15 at 08:02
  • 1
    Hello, have you looked through this post? http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window – mrtna Jul 09 '15 at 08:02
  • Oh ok.. so can you show how you have opened the new page. As this is possible using the window handle. – Sudhansu Choudhary Jul 09 '15 at 08:06
  • @MairajAhmad..Yes it is on the same page – J-J Jul 09 '15 at 08:08
  • @SudhansuChoudhary..the page was opened manually sir – J-J Jul 09 '15 at 08:11
  • Try this `window.top.close();` and also please post the code how you have opened the window, we can see the window handle. – Sudhansu Choudhary Jul 09 '15 at 08:12
  • If the page was opened manually (i.e. not by clicking on any element of the initial page - page [a]) then there is no relationship between page a and page b – jasonscript Jul 09 '15 at 08:12
  • @SudhansuChoudhary...I tried that already sir but didn't work – J-J Jul 09 '15 at 08:13
  • @jasonscript..so meaning there is no possibility for that window to be closed using 'window.close' sir? – J-J Jul 09 '15 at 08:16
  • 1
    @J-J I don't think so. Otherwise malicious websites could look for other tabs ('google' or 'bank' etc) and close the tab. The only tabs you can close are those you've explicitly created via the window handles that get created when you open a window – jasonscript Jul 09 '15 at 08:18
  • I agree with @jasonscript that's why I had been asking you to show how have you opened the new window :) – Sudhansu Choudhary Jul 09 '15 at 08:26
  • Hey instead of manually, can you try to open it through `window.open()` and pass the url, so that we can close it later with the handle. Would that go well with your requirements? – Sudhansu Choudhary Jul 09 '15 at 08:31
  • Closing a window from JavaScript that wasn't opened by JavaScript is pretty much outside what the spec allows (http://www.w3.org/TR/html51/browsers.html#dom-window-close). There are differences in behaviour between browsers and some potential workarounds to try out - just check the SO answer above and others on SO - but they might not be reliable in the long term. – Ian Gilroy Jul 09 '15 at 08:32

2 Answers2

0

Try the following code

function closeCurPage()
{
   self.close();
}

<asp:Button ID="Button1" runat="server" Text="Close" OnClientClick="closeCurPage()"/>
Kuldeep More
  • 138
  • 3
  • 14
0

This is not possible for windows that you have not opened yourself. If it were, malicious websites could look for other tabs ('google' or 'bank' etc) and close them.

The only tabs you can close are those you've explicitly created via the window handles that get created when you open a window. For example:

    // open a window and save the handle to the 'myWindow' variable
    var myWindow = window.open("", "myWindow", "width=1000, height=900");
    myWindow.document.write("<h1 style='width:100%;'>I own this window</h1>");
    myWindow.document.write("<p>I own this window; therefore I can close it!</p>");

    // start a timer and close the window in 2 seconds
    setTimeout(function(){ myWindow.close() }, 2000);
jasonscript
  • 6,039
  • 3
  • 28
  • 43