Object Literal in Firefox extension - javascript

I have an extension that needs to pop up a window and then close it.
var my_extension = {
window: null,
popup: function(){
my_extension.window = window.open(...)
},
close: function(){
my_extension.window.close()
}
}
The popup calls the close function. However, after returning from the open, the my_extension.window is null. I check to make sure it is set in popup. Is another instance of my_extension created when the popup returns?

my_extension is defined in the main browser window, not in the popup. To close the popup from the popup itself, just use window.close
edit: ok, so I guess my_extension.close actually looks something like:
function() {
// check input from popup window
if (everythingIsGood) {
my_extension.window.close()
}
}
In that case, I would recommend you do that validation in the popup itself. I know, you don't want to put a lot of code in the popup window. And I agree. But you can pass in whatever information is necessary to do the validation (including passing a validation function -- remember that functions are objects too, because JavaScript is cool like that!) when you open the popup. Look for the discussion of window.arguments on this page: https://developer.mozilla.org/en/DOM/window.openDialog

Related

Call JavaScript of parent window from child window

I have a calendar and when I click on a <td>, a pop-up window appears so you can create your evenement for the date you selected. I want to add a feature.
When the user finishes creating the event, I want to send a JavaScript request to the parent page so I can refresh the calendar using AJAX. Basically, I want to call a function from the child, but the function is on the parent page.
On Google, I only found a script that can refresh the parent window – nothing about a “parent callback”. ☹ Is it even possible?
P.S. The answer can be pure JS or jQuery, it doesn’t matter. I’ll keep looking in the meanwhile.
What you're looking for is a reference to the window that opened the popup window. Once you have that, you can call functions in that window, read and write variables in that window, or even manipulate its DOM.
That reference is called opener. It gives you the window object for the window that opened the current window. For example, if you have a function in the original window like this:
function updateMe( data ) {
alert( data );
}
then in the popup window you could call it like this:
opener.updateMe( 'Hello!' );
Naturally, you need to make sure that updateMe() is a global function in the original page. Or if you have some object in the original page and updateMe() is a method of that object, you can still do it, as long as the object is global. e.g. in the host page:
var myObject = {
updateMe: function( data ) {
alert( data );
}
};
then in the popup you could do:
opener.myObject.updateMe( 'Hello!' );
Basically, as long as you could get to the object or function in the original page with window.whatever, then in the popup you can simply change that to opener.whatever.

How to call javascript from different window?

I am working with IE9. Want to open a popup window from a page, and from the popup, how to call the javascript code in the page (not the popup window)?
There will not be a cross domain issue as I am going to load a page from the same domain of the page into the popup window.
Like Cory said, use window.opener to get the parent window's window. Look at this:
http://jsfiddle.net/Dmnqk/
function openPopup() {
var w = window.open("", "");
w.document.open();
w.document.write("<script type='text/javascript'>window.opener.parentWindowFunction();<\/script>");
w.document.close();
}
function parentWindowFunction() {
alert("called");
}
Of course, my use of document.open/write/close is simply to create my own page that calls the parent window's function (technically, it should really have <html><head></head><body>SCRIPT HERE</body></html>. All your popup's page would need is the call to window.opener.parentWindowFunction.
You can use window.opener to access functions in the global (window) namespace of the page that opened the popup:
Opener
function functionFromOpener() {
return "wharrgarbl";
}
Popup
alert(window.opener.functionFromOpener());
Obligatory jsFiddle example: http://jsfiddle.net/9yLu2/1/
That said, I think it's a better idea to use in-page DHTML dialogs. (Or possibly it's that browser windows that aren't resizeable / don't have address bars bug me that much.)

How to control opener page which is opened by window.open method

I have a page in which i m calling another popup by window.open method. The only thing how can i change a label in opener page from popup page while the popup page is still alive ie which is not closed yet
It's better to let the opener window take care of changing values by exposing a small API to the popup window.
I've outlined it here: javascript - pass selected value from popup window to parent window input box
It should be like this:
window.opener.document.getElementById('label1').value = "the new value";
<script>
function myFunction() {
var additionalWindow = window.open("/additional");
// Write on the additional window
additionalWindow.document.write('written from separate window');
// Call a function on the additional window
additionalWindow.someFunction();
}
</script>
Here's Mozilla's documentation on window.open().

window.close(); not working when page changed or refreshed

I have this little function to open/close a popup player:
function popuponclick(popup)
{
my_window = window.open("folder/player-itself.htm", popup, "width=350,height=150");
}
function closepopup()
{
my_window.close();
}
I call the functions from HTML anchors that are on each page of the site (idea is to have the player stopped/started whenever you want)...now...
it works well until i change the page, or refresh the existing one - and from then the window can't be closed anymore. Any idea where i'm wrong? Tested in FF and IE8, same behavior.
Thanks for your help.
When you reload the original window (or tab), everything about the old one is gone, blasted into the digital void, never to be seen or heard from again. The bits literally disintegrate into nothingness.
Thus, the "my_window" reference you so lovingly saved when the second window was opened is gone for good, and the "my_window" variable in the newly-loaded window contains nothing. It's name is but a mockery of the variable in the now-dead page.
The only way to deal with this situation is for the popup window to periodically check back via "window.opener" to see if its parent page has been rudely replaced by some interloper. If that happens (and the new page is from the same domain), then the popup page can restore the reference to itself in the new page's "my_window" variable.
edit — OK here's a sample. You'd put something like this in the popup page, not the launching pages:
<script>
var checkParent = setInterval(function() {
try {
if (window.opener && ('my_window' in window.opener))
window.opener.my_window = window;
}
catch (_) {
// clear the timer, since we probably won't be able to fix it now
clearInterval(checkParent);
}
}, 100);
</script>
That's probably pretty close.

javascript-how to open window (from hyperlink) and then close it with delay of 5 sec?

I am trying to open new window from hyperlink using java script and then auto close it in five seconds. It either closes right away or doesn't close at all. Here are some samples of code I was using:
"function closeOnLoad(myLink){var newWindow=window.open(myLink);newWindow.onload=SetTimeout(newWindow.close(),5000);}" + LinkText + ""
You're better off closing the window from the parent instead of defining an onload handler within the child. Due to security restrictions, you simply may not have access to modify child window events.
function closeOnLoad(myLink)
{
var newWindow = window.open(myLink);
setTimeout(
function()
{
newWindow.close();
},
5000
);
};
}
you need to use what is called a 'closure' to wrap the timeout in. It's like the function to timeout and then close is wrapped within another function.
I won't go into detail here, but lookup javascript and closures and play around to see how they work.
Here's a link to help get you started: http://www.jibbering.com/faq/faq_notes/closures.html
The window closing code should be in the window's code:
$(document).ready(function() {
setTimeout(function() {
window.close();
},5000);
})
BUT, you will get a popup asking for the user to confirm if you try & close the popup that way.
To unload is the unload() function. Here you have an example.

Categories