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.
Related
In my project, I have a main page, where the user logs in and sees the main tables, as well as many additional pages, which open in different windows.
When the login timeout, the user should not be able to access any content. In the main page, I give a link to the login page again. In the sub-windows, I can't do that, because I don't want the login to be done in a sub-window.
In javascript, I can use window.opener to have access to the main window (since each sub-window is opened with window.open). How can I do that inside an html a tag?
if (!login_check($conn)) { // tests if the user is connected
// exits with a message, if not connected anymore
exit("<p>You don't have authorization to access this page. Please, <a href='index.php' target='_parent'>go back to main screen and login</a>.</p>");
}
The problem is: this code opens the login page on the sub-window, so target='_parent' does not act like I have thought. What is the proper way to achieve this, if any?
If you assign window.name property of the original window some value (e.g. "master") then you can target that window simply by <a target="master">.
Here is simple proof-of-concept in dataURI format: copy & paste to your location bar, Chrome/Firefox only; hard to simulate any other way here:
data:text/html,<script>window.name="master";</script>I am master. <button onclick="window.open('data:text/html,<a target=\'master\' href=\'data:text/plain,content for master from slave\'>Replace content of master</a>')">Open slave</button>
There is even quite amusing possibility to set masterʼs (openerʼs) name from within slave, but only if both share same origin:
<a onclick='if(window.opener)window.opener.name=this.target=Math.random()' href='whatever'>Open whatever in opener, if any</a>
MDN: window.name
You can't.
target only lets you navigate between frames in the current window/tab.
Only JavaScript has access to the opener. I haven't tested the following, but it should point you in the right direction.
document.querySelector('.login-expired a').addEventListener('click', open_in_opener);
function open_in_opener(event) {
window.opener.location = this.href;
event.preventDefault();
}
<p class="login-expired">You don't have authorization to access this page. Please, <a href='index.php'>go back to main screen and login</a>.</p>
Target is for frames and iframes. You need to handle with javascript like this
<a href="#" onclick="window.opener.location.href = 'index.php'; return false;">
Good luck
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.
Trying to open a link in a framed page and close the child window. The link bellow is in a child window and when I click it opens the link in the framed page, but did not close the child window
<a target="Resultado" href="?Tela=1"
onClick="javascript:return confirm('TryMe');window.close();">
I have used a code like this to close the window... but couldn't get it to work with the above code.
<a href="javascript:window.opener='Resultado';window.close();">
Try this:
<a target="Resultado" href="?Tela=1" onclick="clickHandler(event, this);">Link</a>
And declare this:
function clickHandler(e, el) {
var choice = confirm('TryMe');
if (!choice) {
e.preventDefault();
}
window.close();
}
I wasn't sure of your original use of window.close() since it came after the return and would never execute, so it's up to you to move it to where you want.
Create another webpage on your webserver & use that as the Custom URL thankyou page for your form.
In that new webpage have just one line of code.
<body onload="javascript:window.opener.childClosed();window.close();">
--
That code will call a javascript function in the parent window and then close the child popup; you can then use that javascript function to do a redirect on your parent page.
I am not to sure where the URL of your 'Thank you for requesting...' page is, but here is some javascript to redirect to google on your parent page after the form is submitted.
function childClosed() {
window.location = "http://www.google.com/"
}
You can put that script before your closing <body> tag.
--
Hopefully I have understood your query OK, let me know if you have any questions or need any clarification on this possible solution.
-
I have made a very basic clone of your webpage & form here if you want to test out the functionality, 'Factoring Question?' is the link that contains this code.
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" [...] >
Can Anyone tell me the difference between window.location.href and top.location.href ?
And also where to use which one.
And which one will be better when redirecting after an ajax call in mvc?
window.location.href returns the location of the current page.
top.location.href (which is an alias of window.top.location.href) returns the location of the topmost window in the window hierarchy. If a window has no parent, top is a reference to itself (in other words, window === window.top).
top is useful both when you're dealing with frames and when dealing with windows which have been opened by other pages. For example, if you have a page called test.html with the following script:
var newWin=window.open('about:blank','test','width=100,height=100');
newWin.document.write('<script>alert(top.location.href);</script>');
The resulting alert will have the full path to test.html – not about:blank, which is what window.location.href would return.
To answer your question about redirecting, go with window.location.assign(url);
top object makes more sense inside frames. Inside a frame, window refers to current frame's window while top refers to the outermost window that contains the frame(s). So:
window.location.href = 'somepage.html'; means loading somepage.html inside the frame.
top.location.href = 'somepage.html'; means loading somepage.html in the main browser window.
Two other interesting objects are self and parent.
The first one adds an item to your history in that you can (or should be able to) click "Back" and go back to the current page.
The second replaces the current history item so you can't go back to it.
See window.location:
assign(url): Load the document at the provided URL.
replace(url): Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
window.location.href = url;
is favoured over:
window.location = url;
top refers to the window object which contains all the current frames ( father of the rest of the windows ). window is the current window.
http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific
so top.location.href can contain the "master" page link containing all the frames, while window.location.href just contains the "current" page link.