Javascript 将值从弹出窗口传递到其父窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22075738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:22:28  来源:igfitidea点击:

Javascript Passing value from a popup window to its parent window

javascriptparent-childpopupwindow

提问by user2044626

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

我是 javascript 新手,我被困在这里。假设我有一个父窗口,它有一个输入字段以及一个打开弹出窗口的按钮。此弹出窗口包含图像。当我单击弹出窗口上的任何图像时,我只需要将图像 URL 复制到父窗口中的该输入。请任何帮助。谢谢

Edit:

编辑:

This is my javascript code on the popup window

这是我在弹出窗口中的 javascript 代码

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

}
</script>

HTML

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

父窗口 HTML 代码

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

I Just Can't get it to work

我只是无法让它工作

回答by user2044626

I finally figured it out Use window.opener

我终于想通了 使用 window.opener

window.opener.document.getElementById('archiveimages').value = address.src;

回答by MoustafaAAtta

When the user select the image from the popup, use the following statement to set the value of the input field. Assuming the id of the input field is 'myInputField'

当用户从弹出窗口中选择图像时,使用以下语句设置输入字段的值。假设输入字段的 id 是 'myInputField'

window.parent.document.getElementById('myInputField').value = imargeUrl;

回答by timi2shoes

You can open a window using:

您可以使用以下方法打开一个窗口:

window.open('http://www.website.com/popup/page?var=test','Note','width=700,height=500,top=150,left=150,scrollbars=yes'

Then when you click a link on the popup you can do:

然后,当您单击弹出窗口上的链接时,您可以执行以下操作:

goback('http://www.website.com/page?var=testing');

回答by QBM5

window.parent.getImageURL(img.src);

The popup can access it parents javascript with the above method, just have a function (like getImageURL()) on the global scope and it will work

弹出窗口可以使用上述方法访问它的父 javascript,只需在全局范围内有一个函数(如 getImageURL()),它就可以工作