javascript - How to run script in parent window for child window? - Stack Overflow

admin2025-04-19  0

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(''); popUp.$('[value="Submit"]').click(); //this line should be fixed

How could do this?

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?

Share Improve this question edited Jan 25, 2019 at 13:17 Kunj 2,0182 gold badges23 silver badges34 bronze badges asked Jan 25, 2019 at 11:12 rashrash 537 bronze badges 1
  • Actually your line should work (assuming you're on the same domain so that you have access to the child window). What error message are you getting? – Bergi Commented Jan 25, 2019 at 12:47
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745041948a281595.html

最新回复(0)