how to close a current page in chrome using Javascript

Devos2022 21 Reputation points
2022-01-10T22:42:33.437+00:00

I have developed a chrome extension and I would like to close current page where the getClickHandler was fired

below the background.js file

chrome.contextMenus.create({
"title": "My App",
"type": "normal",
"contexts": ["all"],
"onclick": getClickHandler()
});

function getClickHandler() {
return function (info, tab) {
//do a job it will open a new page
// Create a new tabto the info page.
chrome.tabs.create({ url: url, });

---------------------Here I want to add script to close the current page not the create tab thenew page---------------
};
};

I tried window.close but it did not work
please help me to close the current page ..Thanks:)

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,096 Reputation points
    2022-01-11T05:10:21.033+00:00

    Hi @Devos2022 ,
    Ordinary javascript cannot close windows willy-nilly. If you want to use js to close the windows,you must open it first. One way,you could use Chrome extension.Just like this:

    chrome.windows.remove(integer windowId, function callback)  
    

    Another,if you use the old Chrome,you could do like this:

    open(location, '_self').close();  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.