javascript 仅使用 JS 和 HTML 从弹出窗口中获取价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16421922/
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
Getting Value From Popup Window Using only JS & HTML
提问by Craig Wayne
Is it possible to get a value from a popup window? Additionally I would like to use JS and HTML only, i.e. no PHP. Is this even possible? I've seen other posts on here like this one:
是否可以从弹出窗口中获取值?另外我想只使用 JS 和 HTML,即没有 PHP。这甚至可能吗?我在这里看到过其他类似这样的帖子:
getting value from popup window
but that's in aspx.
但那是在 aspx 中。
I have googled a bit and found this link:
我用谷歌搜索了一下,找到了这个链接:
http://www.bignosebird.com/js/popmap.shtml
http://www.bignosebird.com/js/popmap.shtml
However, it works on that guys site but not when i copy and paste it, I may be a noob so this is what I have:
但是,它在那个家伙的网站上有效,但在我复制和粘贴时无效,我可能是个菜鸟,所以这就是我所拥有的:
parent.html
父.html
<html>
<head>
</head>
<body>
<form>
<INPUT TYPE="TEXT" NAME="maparea" SIZE=2 VALUE="">
<input type=button onClick='targetitem = document.forms[0].maparea; dataitem = window.open("map.shtml", "dataitem", "toolbar=no,menubar=no,scrollbars=yes"); dataitem.targetitem = targetitem' value="Show Map">
</form>
</body>
</html>
map.shtml
地图.shtml
<html>
<head>
<script>
function select_item(item)
{
targetitem.value=item;
top.close();
return false;
}
</script>
</head>
<body>
<CENTER>
<B>Our Map</B>
<BR>
<IMAGE SRC="map1.gif" ISMAP USEMAP="#MAP1">
<MAP NAME="MAP1">
<AREA SHAPE=RECT COORDS="11,10,116,133" HREF="" onClick='return select_item("1")'>
<AREA SHAPE=RECT COORDS="121,11,227,172" HREF="" onClick='return select_item("2")'>
<AREA SHAPE=RECT COORDS="11,140,115,226" HREF="" onClick='return select_item("3")'>
<AREA SHAPE=RECT COORDS="119,177,225,227" HREF="" onClick='return select_item("4")'>
<AREA SHAPE=default HREF="" >
</MAP>
</CENTER>
</body>
</html>
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by jgpATs2w
It works for me (using Firefox 20.0). But the code is really ugly and old though, probably you should study the present standards of MSDN Windowto understand how it works in firefox (Window object may change its behaviour in other browsers). Ah, and of course the ECMAScript. But to introuduce one of the multiple solutions, you could try this:
parent.html
它对我有用(使用 Firefox 20.0)。但是代码真的很丑很旧,也许你应该研究MSDN Window的当前标准来了解它在 Firefox 中的工作原理(Window 对象可能会改变它在其他浏览器中的行为)。啊,当然还有ECMAScript。但要介绍多种解决方案之一,你可以试试这个:
parent.html
<input type="text" id="output"/>
<button id="show">Open</button>
<script>
document.getElementById('show').addEventListener('click', function(){
window['output'] = document.getElementById('output');
window.open('map.html')
});
</script>
maps.html(I changed the extension!)
maps.html(我更改了扩展名!)
<input type="text" id="user_text"/>
<input id="send" type='button' value'send'/>
<script>
document.getElementById('send').addEventListener('click', function(){
window.opener['output'].value = document.getElementById('user_text').value;
})
</script>