Refresh parent window after closing a popup window - javascript

I am opening a popup window from X.jsp and the popup is represented by Y.jsp. The second jsp has a button. What I would like to do is to refresh the parent page (the one that launches the popup) once this button is hit. The Save button below will call a method in the backing bean "copyScriptForVersion" and I close the popup on button click. How could I also refresh the parent window after closing the popup?
<h:commandButton type="submit" value="Save" action="#{copyScriptForVersion.SaveAction}" onclick="window.close()"/>
I tried adding window.opener.reload() like this onclick="window.close(); window.opener.reload();", but that didn't work.
I also tried adding the following
body onUnLoad="window.opener.location.reload(1);">
But, I got "window.opener.location is null or not an object" error.

First of all, you should do window.close() as a LAST action in the onClick handler. Otherwise, it's possible it will never get executed.
Second, if reload() doesn't work, try:
window.opener.location.href = window.opener.location.href; window.close();
Please note that this may cause problems if the parent page was produced as a result of POSTing a form.

Try this it may help other as well,,
`new = window.open(url);
new.focus();
check = setInterval(function() {
if (new.closed) {
window.location.reload(true)
}}, 1000);`

Related

Cypress - How to redirect without opening a new window

I have a cypress test that on clicking a button the system redirects to a new window.
I've tried some solutions (invoke,stub) without success.
I need to do some testing but I need it to always be redirected on the same screen as cypress doesn't support multiple windows.
My button code:
<button id="cmdEnviar" name="cmdEnviar" type="button" onclick="TrPage._autoSubmit('_id5','cmdEnviar',event,1);return false;" class="x7j">Enviar</button>
My code:
it('register new user', function() {
cy.visit('/')
cy.get('#txtLogin').type(Cypress.env('login'))
cy.get('#txtSenha').type(Cypress.env('pass'))
cy.get('#btnEnviar').click()
cy.get('#cmbSistemas').select(11)
cy.get('#cmdEnviar').click() //here is the button
//here is redirected to another window
cy.contains('Colaborador').click()
cy.contains('Cadastro').click()
})
Could anyone help me, if there is any solution?
Unfortunately, without seeing what your onclick event actually does, I can't recommend a strategy for validating the behavior of onclick is functioning as intended.
Instead, we can validate that the button has the correct onclick attribute value.
...
cy.get('#cmdEnviar')
.should('have.attr', 'onclick', "TrPage._autoSubmit('_id5','cmdEnviar',event,1);return false;");
...

CasperJS: How to click a link in a frame that closes the page, without error

I have a form I'm filling and one of the fields requires picking an object from a list in a popup window. The list is inside an iframe on the popup page, and clicking the link in the list closes the popup and places the value in the form on the original page.
My problem is with my setup I'm clicking the link within the iframe kills the popup, and then on exiting the this.withFrame() function I get the error
Error: cannot access member 'switchToParentFrame' of deleted QObject
Here's roughly what my code looks like.
this.withPopup(/popup/, function() {
this.withFrame('listFrame', function() {
// do stuff to get the right link selector
this.click(link);
});
});
If there's a way to click the link from outside the withFrame() function?
A workaround would be to use the PhantomJS functions page.switchToFrame() and page.switchToParentFrame() and explicitly check if the page is still valid. Maybe a check is not even necessary, because the page instance is already destroyed.
this.withPopup(/popup/, function() {
this.page.switchToFrame('listFrame');
this.click(link);
});

"onclick" of a button in a popup works only once

I have an html/php webpage (the file is called searchresults.php) that imports jquery mobile. When you enter the page, the url is usually something like
www.domain.com/searchresults.php?&sort="off"&max="5"
In this example sorting is off and only 5 items are displayed. On that page is a button that opens a popup where I want the user to be able to change these settings. I use the built-in jquery mobile popup. On that popup you can toggle "sort" on/off and you can enter a new maximum. On the popup is an "ok" button to confirm your new settings. It looks like this:
OK
The sortAgain(); function in javascript looks like this:
function sortAgain();
{
//some code to get the necessary variables//
...
//change the href of the button so you reload the page
document.getElementById("okbutton").href = "searchresults.php" + "?keyword=" + var1 + "&sort=" + var2 + "&max=" + var3
}
So, basically, right before the "ok" button navigates to another page, I set its href of the page where it should navigate too.
This scheme works, and the searchresults.php file is fetched again from the server and re-interpreted (with the new variables in the url).
However, if i try to change the settings again after changing it once, the popup just does nothing! In other words, the href of the ok button on the popup stays empty and the javascript function sortAgain() is not called. I don't understand why it calls the onclick method perfectly fine the first time, but then refuses to call it again?
I assume it has something to do with the fact that the popup html code is an integral part of searchresult.php file, and that hrefing to the same page gives problems? The popup is pure html, no php is involved in the popup code. and again, it works fine the first time.
You should check out how to attach events via JavaScript. See here: javascript attaching events
You need to use the "pageinit" event when setting up click handlers in JQM: http://api.jquerymobile.com/category/events/
Here is an example of how to bind click handlers when the page is first loaded.
$( document ).on( "pageinit", "#that-page", function() {
$('#okbutton').on( "click", "#that-page", function( e ) {
$(this).attr("href", "searchreslts.php");
});
});

How to submit parent window form when child window closes

I have a gridview displayed in a page which also has a link. When I click on the link, a showModalDialog() will open up where I can edit the value.
After that when pressing Update button the value is getting updated in the database and my child form closes perfectly.
But I need to update my parent page table as well. So that the new value gets reflected in the parent form. I tried the following in the child window.
<body onunload="window.opener.document.forms[0].submit();">
But that is not working. Suggest me a good solution.
You do not have onUnload in a modal dialog
var res = showModalDialog(...); // execution is blocked until modal is closed
if (res) location.reload(1); // res contains what dialog set returnValue to
and in modal dialog you do
window.returnValue=true; // if ok submission
window.close(); // will return control to opening window
Anyway, did you look to the right? there are many duplicate questions about gridviews and modal dialogues
I guess you have a function on Update button click, which updates the database and close the form, can you place that code (the one that you have in unload) in that function before closing the window

PHP and window.close in JavaScript

I have a problem. I have a page that when you click a button, a popup with a form is shown. So, I complete some data and I submit. What I want to do is, to submit the form, close the form and refresh the parent page. I don't want to do it with AJAX.
The problem is that in my parent page I have to refresh content with the input information of the form.
So when I refresh, sometimes the data is shown and sometimes not. Do you know why this could happen?
I just use onsubmit="refreshParent()" in my form. The info is stored always in my database, so I think the problem may be that sometimes the refresh catches the new info and sometimes not.
function refreshParent() {
window.opener.location.reload();
window.close();
}
I use this to reload the page that opened a popup window:
<script language="JavaScript">
<!--
function reloadParentPage() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow) {
window.opener.progressWindow.close()
}
window.close();
}
//-->
</script>
By the way, the code above is called by a link or button in the popup page.
You have a race condition between the script doing the insert and the script reloading the parent.
The solution is to call refreshParent on the page after the submit - that way you know the data is in the database. You don't even have to do it on document ready - return a stub page that just defines and calls refreshParent in the head tag.
In PHP when you run post script, at the end, include this code :
echo '<html><script language="javascript">
parent.location.href="http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'"; // or any other url
</script></html>';
This will output a javascript that will reload the windows.

Categories