How to pass data back to parent window? - javascript

I found a link that shows how to send parameters back to the opener browser window. But it works only with window.showmodal(). There should be an easy way to do the same if I use window.open() right?

If you are using window.open, you can call a function in the parent window and pass in pararmeters to that function.
Do this in child page javascript
var x="hello";//parameter I want to pass to the parent
window.opener.ParentPageFunctionName(x);

You want the parent property. See this for details:
http://www.w3schools.com/jsref/prop_win_parent.asp

Related

How to run script in parent window for child window?

My child.html have a button which could click using script $('[value="Submit"]').click();
I want to invoke this from parent.html
Something like this:
var popUp = window.open('https://child.html');
popUp.$('[value="Submit"]').click(); //this line should be fixed
How could do this?
You should use window.postMessage API. Here is example code.
child.html
function functionToBeExecuted(event)
{
// do click or whatever you want
}
window.addEventListener("message", functionToBeExecuted, false);
parent.html
let child = window.open('child.html');
child.postMessage(); // notice that I did not pass message argument, since you just want to trigger the function, but if you want to pass some data to function using event parameter, just pass data to this function (for example string or object etc.)
I went in a similar kind of issue where I had iframe(containing child html) inside my parent html.
I solved this $(".DivClass iframe").contents().find("button").trigger('click');
In your case its pop window I don't no which popup you are using: custom pop or browser pop.
You can Add iframe inside your popup to achieve this issue using the same code as what I did.
Condition is that popup should be custom html popup not the other popup which is provided by browser provide.
Use this code in parent.html to click button:
let child = window.open('child.html');
let doClick = () => child.document.getElementById('my-button').click();
child.addEventListener('load', doClick(), true);
This is what you want... But my suggestion is to make function on child and use window.postMessage API to trigger it...

How to refresh the parent page and post value from child window in javascript/jquery?

Does anyone know how to refresh the parent page and post value from child window in javascript/jquery?
I have using window.opener.location.reload();to refresh the parent page, but I have no idea how can I also passing the post value ?name=test&age=28 while refreshing the parent page.
try,
window.opener.location.href = window.opener.location.href + "?name=test&age=28"
This should reload the parent page with the added values.
You could set the window's href attribute.
e.g.
window.opener.location.href += "?name=test&age=28"
Depending on the browser you may or may not need to still include the reload code.

Javascript post back of parent page from child page

On Button Click I am calling this JavaScript Method from Child(aspx) page.
function closechildwindow() {
window.opener.document.forms(0).submit();
self.close();
}
Its closes the child Page and its also makes a post back on the parent page. But the parent page is open in a separate Tab.
I don't want this. I want that post back to happen on parent page in same tab that is already open, and should not open in new tab.
Please help me
What you need is calling the __doPostBack() Javascript function on the parent window:
window.opener.__doPostBack();

Is there a way to find the window of the previous page (other tab)?

If you open a link in a new tab (with MMB or right click or preferred method, however), is there a way from the new tab window to find the opener window? These don't work:
window.opener
window.parent
Maybe it's just not possible, since following a link usually 'overwrites' the window, so no ref is possible, but since we have tabs... maybe... a ref IS possible?
I want the opened window to do a postMessage to the original window to let it know the page has loaded, so the original window can update the link style.
I don't make these pages, so I don't have access to the back-end or HTML printage. Think Greasemonkey: I add JS to existing websites to improve them.
The only thing I can think of is intercepting the click event, grabbing the URL, and passing it to window.open and subsequent postMessage calls on the returned window object. Do you generate the page with these links?
In some browsers window.open(url,'_blank') will open in a tab.
I think there is two solutions to your problem
First solution : Cookies
An easy way is to set a cookie with the value of the current page everytime a link is clicked, you can use a library like this one to set a cookie and get it in the next page once you click like this
$("a").click( function () {
$.setCookie("currentPage",document.URL);
});
In the next page you just get the cookie like this
if($.getCookie("currentPage") != undefined )
var previousPage = $.getCookie("currentPage");
Second solution : Server-side langage
If you're using a server side langage like php, it's easy to get the url of the previous page :
<?php
if (isset($_SERVER['HTTP_REFERER']))
echo "<script> var previousPage='" . $_SERVER['HTTP_REFERER'] . "'; </script>";
?>
And finally, to post a message you just need to test if the previousPage variable is set:
if(previousPage != undefined)
{
// Code to post your message
}

How can I call parent window’s function sb(frm) and pass parent’s form from child window?

I have the following script in the window that opened a current window:
function sb(frm){….}
How can I call parent window’s function sb(frm) and pass parent’s form from child window?
I want to do something like that
if(sb(parent.form)){…}
The code I tried
var zzz = window.opener.$("#frmevent");
if(sb(zzz))
zzz.submit();
MY HTML:
<form action="app_editevent.jsp?nextlevel=1&" method=post onsubmit='return sb(this);' name=frmevent id='frmevent'>
The right tool to deliver messages between parent/child windows (iframe) is postMessage otherwise you're risking having your code detected as "problematic" from security perspective when the parent and the child are not located on the same domain.

Categories

Resources