MSIE11, popup window using window.close() erases a variable just set - javascript

SUMMARY:
In MSIE 11, in a popup window when I fill a certain field with data it stays filled, or not, depending on if the popup window has window.close() called on it. Can't repeat with other browsers.
DETAILS:
I have a popup web page (a JSP page, if it matters to someone) I use to fill existing fields in a web form. I added a new field and it is filling OK -- sometimes.
Here is the pertinent section of the page, cleaned up for presentation. My actual page doesn't hard-code emails in the Javascript object.
function fillForm() {
var parentWindowOrder = window.opener.salesOrder;
var formData = parentWindowOrder.workingData;
[snip]
var xferEmails = {"emails":["jerome#myserver.com","jerome2#otherserver.com"]};
formData.user_emails = xferEmails.emails;
[snip]
}
window.close(); // HERE IS THE TOGGLE OF MY PROBLEM
In MSIE, if window.close() is disabled (commented out) then everything works OK. Elsewhere in the page the formData.user_emails can be referenced OK. Of course, the popup page still exists and must be manually closed. When using the console the query yields:
>formData.user_emails
["jerome#myserver.com","jerome2#otherserver.com"]
In MSIE, if window.close() is executed in the popup then the popup closes, of course. However, the values stored aren't preserved. When in the popup window itself, either before or after calling window.close(), the field is set and stays set. But after that the values are gone:
>formData.user_emails
{}
In other browsers I get the success condition.
Normally you'd say "You're changing the value elsewhere in your code"; however, I've already swept through my code looking for other references. Here, shown above, is the only place it gets set in my scenario.
In my practice other fills of variables through JSON-like syntax (xferEmails) works OK, even in MSIE. Not this particular one. What with window.close() could influence this?
Thanks for replies,
Jerome.

formData is initialized in the popup opened and within the scope of fillForm function. This is assigned to salesOrder variable of parent window. Did you re-initialize formData with salesOrder in parent window? If you didn't then it should be accessible via salesOrder.

Related

Javascript : get current window.open() when parent window was refreshed

I want open window but when user refreshed parent location or going to hyperlink, we got last child window and using this code win_pop.location.replace('http://example.com') .
for example :
var win_pop = window.open(url, 'lastWin', 'width=300px,height=200px,top=1200px,left=2000px');
why after refresh parent location child window doesn't working.! before any refresh or going to hyperlink in parent page that working well.
win_pop.location.replace('http://example.com') !!
Do you got any best suggest ?
You load a page
You assign a value to a variable called win_pop
You load a new page
The lifespan of a JavaScript program running in an instance of a webpage is only as long as that webpage instance exists.
When you have a new page, you have a new JS environment, and the old data is lost.
win_pop no longer contains the value you assigned to it.
Much of the time you can store data between pages (e.g. via Local Storage), but that only applies to simple objects, arrays and primitives.
There is no way to preserve the reference to a new window.
The only way to achieve that effect would be to avoid reloading the original page. For example, by performing all navigation to "new pages" by using Ajax and manipulating the history with pushState and friends.
#Quentin is right about what happens to your js variable, but he seems to have forgotten a little thing:
You can make use of the postMessage API and the MessageEvent.source property which will point to your popup Window.
So you can start a loop on your popup which will post a message every n-th seconds to window.opener.
setInterval(function(){
window.opener.postMessage('foo', '*');
}, delay);
Then in all your pages, you will listen for the message event, and set your popup variable to the MessageEvent.source you just retrieved:
onmessage = function(evt){
popup = evt.source;
};
And voilà!
Here is a plnkr

Reload parent page after closing popup window

I am trying to have the user sign in through a popup window. When they click the link to the popup window which is a php variable they can sign in. When the window closes I want it to reload the page they were originally on (the parent page).
Here is the code on the signin.php page...
<body onunload="opener.location=('')">
But all this does is make the sign in page become the page the user was on. I think I need to put something in the parentheses, but I can't figure out what goes there.
To reload a page, you can set the location property as the current value, like this:
window.location = window.location;
So for your case, you would use, literally:
onunload="window.opener.location = window.opener.location;"
You can also use the reload method of the location object:
onunload="window.opener.location.reload();"
This is the preferred method.
Also, please refer to the accepted answer for your previous question: Refreshing Parent window after closing popup
Documentation
window.location on MDN - https://developer.mozilla.org/en/DOM/window.location
window.opener on MDN - https://developer.mozilla.org/Talk:en/DOM/window.opener
echo '<script>window.opener.location.reload()</script>';
echo '<script>self.close()</script>';
It works well in all browsers.

How do I transfer a value from a text box to a new window using Javascript?

I have created a form so the user can complete his name, last name, credit card number etc.
All the information is provided by the user.
When the user has ended his work he should press a button and a new window pops up with the user's information revised.
Anyone has a suggestion on this? I would be grateful.
opener.document.getElementById("text").value = txt;
You can reference the child window from the parent one and vice-versa. When you open a popup window with window.open(url, options), it returns a reference to the child window. You can add handlers to it. Using jquery:
var w = window.open(url);
$(w).load(function() {
alert("I've just loaded");
});
From the child window, you can use the variable opener to reference the parent window. So if you want to grab info from the parent window, you'll use something like (again jquery):
var first_name = $(opener.document).find('.first-name').text();
Got this info here: http://sharkysoft.com/tutorials/jsa/content/053.html
From a user experience point of view, if you just want to open a popup to confirm the data, without server side processing, I find opening a popup disturbing and would rather use an overlay on the same page.

Access a window by window name

If I open a window using
window.open('myurl.html', 'windowname', 'width=100,height=100');
How do I refer to the new window (from the same page that opened it) using 'windowname'? This question is specifically about this. I'm aware that I could save a reference to the handle by using "var mywin = window.open(...)" but I don't care about that in this situation.
Thanks, - Dave
In firefox (might work in other browsers too, but now it's not my concern) I was able to reference one window accross multiple page loads with
var w = window.open("", "nameofwindow");
This opens new window if it doesn't exist and return reference to existing window if it does exist without changing contents of the window.
With jQuery I was then able to append new content, to make quick collection of interresting links like this
$('body', w.document).append(link_tag);
If you didn't save a reference to the window then there is no way to restore it. However, if that window is still open and if the page loaded there belongs to the same domain as your page, you can run JavaScript code in it:
window.open("javascript:doSomething()", "windowname");
Whether that's sufficient in your scenario depends on what you are trying to achieve.
Petr is correct:
var w = window.open("", "nameofwindow");
works in all browsers, I am using it to retrieve the reference to the window object previously opened by a different page. The only problem is the initial opening of the page, if the popup does not exist, you will get a new window with a blank page.
I tried invoking a Javascript function inside the context of the other document in order to check whether I opened a new window or retrieved the already active page. If the check fails, I just invoke window.open again to actually load my popup content:
var w = window.open("http://mydomain.com/myPopup", "nameofwindow");
Hope that helps.
It is not possible. The windowName is just to be used in target="..." of links/forms or to use the same name again in another window.open call to open a new url in that window.
Try open that window with the name, but URL is '' again, to check if it's a blank window or not. If it's in open, then you will get the window; if not, a new window open, and you need close it.
Add the children in localStorage will help to prevent to open the new blank window.
Please check my code in https://github.com/goldentom66/ParentChildWindow
Sorry I am posting late, but if you still have the other window open, and they are on the same domain, you can run, on the first window:
function getReference(w) {
console.log('Hello from', w);
}
And on the second window:
window.opener.getReference(window);
afaik there's no way like windows['windowname'].
The 'windowname' assigned in window.open() can be addressed as a target in <a target="windowname" [...] >

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.

Categories