1

From my webpage, I am opening a new page in different tab. When the page in different tab will be loaded, I want to close my webpage i.e. Suppose I am on pageA and I opened pageB using window.open(). Now, when pageB will be opened, I want to close pageA. I tried this jsFiddle -

function onClickBtn()
{
   var win = window.open('http://www.google.com','_blank','');
    setTimeout(function () {
        win.close();
    }, 5000);
};

This is my HTML markup -

<input id="btn1" type="button" value="Click me" onclick="onClickBtn()"/>

However, this code is closing pageB ,not PageA.

I have tried a jsFiddle https://jsfiddle.net/gdso9eeg/
Please suggest me a suitable solution.

DfrDkn
  • 1,270
  • 2
  • 16
  • 23
  • http://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome – vikingben May 28 '15 at 20:27

3 Answers3

0

It is doing what it should do. You opened the new window with that variable and on using close() with it, it is closing the window which it opened. If you want to close the parent then open the new page on the parent. Try this:

In place of _blank put _parent

Rahul Kumar
  • 99
  • 3
  • 10
  • Its still not working with the code as you suggested - function onClickBtn() { var win = window.open('http://www.google.com', '_parent', ''); win.close(); }; – DfrDkn May 28 '15 at 03:08
  • I want to close the same window on which I am working after a certain time period once the save operation is complete in the backend. – DfrDkn May 28 '15 at 03:13
  • Make _parent the second argument, currently your code has it as the third argument. I have tested it and its working. – Rahul Kumar May 28 '15 at 13:58
  • Now I am not getting what are you trying to do. You want to close the window which you opened on the click event after certain time period? Or the main window from where you opened the next window by click event? – Rahul Kumar May 28 '15 at 14:04
0

If page is redirecting on the same page window then window.close will not work due to some browser security for this scenario type we need to use one of the following way.

1.window.history.back();

2.document.referrer

3.Request.UrlReferrer server side

WitVault
  • 23,445
  • 19
  • 103
  • 133
Sheo Dayal Singh
  • 1,591
  • 19
  • 11
-1

The .close() should follow Javascript's window, not win.

Try

window.close();

Good luck!

EDIT:

Have you checked this post window.close and self.close do not close the window in Chrome ?

"..javascript must not be allowed to close a window that was not opened by that same javascript."

It also offers some workarounds to the issue.

Community
  • 1
  • 1
  • I have taken the opened window in "win" variable. And then calling win.close(), which is working. However, instead of closing current tab, it is closing newly opened tab. – DfrDkn May 27 '15 at 19:18