Andreas Ch.
Andreas Ch.

Reputation: 203

chrome extension development close openned window from javascript

When developing a chrome extension I managed to open a new tab by using this code in my javascript file

myWindow=window.open("www.google.com");

I could close it immediately after opening it with:

myWindow.close();

I tried several methods for javascript for it to wait some seconds before closing but if I do that it doesn't close. Maybe it's because it loses the window's id? I don't know. I just started learning about chrome extension development.

[EDIT] I am submitting all of the code I have to help you guys. I remind you that I am trying to develop a chrome extension and the action happens when I click on the button on my toolbar created by my extension. (Code taken from here: http://developer.chrome.com/extensions/getstarted.html)

HTML FILE

<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
  body {
    min-width: 357px;
    overflow-x: hidden;
  }

  img {
    margin: 5px;
    border: 2px solid black;
    vertical-align: middle;
    width: 75px;
    height: 75px;
  }
  </style>
 <script src="popup.js"></script>
</head>
<body>
</body>
</html>

MANIFEST

{
"manifest_version": 2,

"name": "myExtension",
"description": "This extension is under development",
"version": "1.0",

"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["www.google.com"]
}

Upvotes: 1

Views: 199

Answers (2)

rsanchez
rsanchez

Reputation: 14657

You seem to be doing this from the extension's popup. Popups are closed when they loose focus, so my guess is that what's happening is that its execution environment is being destroyed when your new window gets the focus. So any delayed event you set up there can't be executed.

It seems your popup is an empty window and the only thing it does is opening the window. In that case you don't need to define a popup. You can do that from a background page using the chrome.browserAction.onClicked event.

If you do need to start the action from your popup, you can define a function in your background page, and invoke it using chrome.runtime.getBackgroundPage(function(w){w.yourFunction();}).

Also, keep in mind that in an extension you have the chrome.windows and chrome.tabs APIs available.

Upvotes: 1

nietonfir
nietonfir

Reputation: 4881

This can't be delayed due to the browsers security framework. To open/close a window a user initiated event has to happen beforehand.

Upvotes: 0

Related Questions