PDA

View Full Version : Javascript Passing value from a popup window to its parent window



zagarof2005
02-27-2014, 07:29 PM
down vote favorite


I'm new in javascript and I'm stuck here. Let's say I have a parent window and it has an input field as well as a button that opens a popup window. This popup window contains Images. I just need to copy image URL to that input in the parent window when I click on any image on the popup window. Any help please.

This is my javascript code on the popup window


<script type="text/javascript">
function clicked(address) {
window.parent.document.getElementById('copy_img').value = address.src;

}
</script>

HTML


input type="text" name="copy_img" id="copy_img" size="133" />
<img border="0" onclick="clicked(this)" src="images/1.jpg" width="135" height="46">
<img border="0" onclick="clicked(this)" src="images/2.jpg" width="128" height="48">
<img border="0" onclick="clicked(this)" src="images/3.jpg" width="305" height="44">

Parent Window HTML Code


<input type="text" name="copy_img" id="copy_img" />

I Just Can't get it to work!. Any help please. Thanks

jscheuer1
02-28-2014, 07:43 PM
Looks workable except a parent contains a frame. If you are popping up a window, it's "parent" (more accurately the window that generated it) is it's opener:


<script type="text/javascript">
function clicked(address) {
window.opener.document.getElementById('copy_img').value = address.src;

}
</script>

And, for cross window scripting you need to observe the same origin policy. This means that both the opener and the popup have to be on the same domain.

There could also be other problems.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.