rash
rash

Reputation: 53

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?

Upvotes: 5

Views: 1747

Answers (3)

ajaleksa
ajaleksa

Reputation: 376

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...

Upvotes: 2

ajaleksa
ajaleksa

Reputation: 376

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.)

Upvotes: 3

user3563774
user3563774

Reputation: 47

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.

Upvotes: 2

Related Questions