Want to run javascript function from parent window in child window
Example
I have to different websites let’s say site1.com and site2.com
I want from site1.com to open new window of URL site2.com
new_window = window.open("site2.com/index.php", "window2");
Then i want to run a js function test() on this new_window.
// Where site2.com/index.php
<html>
......
</html>
<script>
function test(){
// some code here
}
</script>
Summary
Want to open new window (child) and run some JS functions from parent window.
You can use cross document messaging to circumvent the same origin policy as follows.
Parent window (
site1.com):Child window (
site2.com):This is a simple example. You could make it more sophisticated to allow callbacks, return values, etc. but that would require a blog post to be fully explained.
You may read more about
window.postMessageon the Mozilla Developer Network.Note however that the following example will not work in Internet Explorer (obviously).