admin管理员组文章数量:1401673
Hi i want to pass the value by calling the parent link page value to its iframe link page sample code is
<html>
<head>
<title>
</title>
<script>
function sendvalue(v){
alert(v);
}
</script>
</head>
<body>
<form id="f">
<input type="button" value="123" onclick="sendvalue(this.value)">
</form>
<iframe id="frame1" src="a.html" width="500px;"></iframe>
</body>
</html>
a.html
<html>
<head>
<title>
</title>
<script>
</script>
</head>
<body>
<h3> Frame</h3>
<form id="myform">
Req value <input type="text" name="a" id="a">
</form>
</body>
</html>
when i call sendvalue function then the value should pass to iframe link page in form for input field. how this can be achieved.
Hi i want to pass the value by calling the parent link page value to its iframe link page sample code is
<html>
<head>
<title>
</title>
<script>
function sendvalue(v){
alert(v);
}
</script>
</head>
<body>
<form id="f">
<input type="button" value="123" onclick="sendvalue(this.value)">
</form>
<iframe id="frame1" src="a.html" width="500px;"></iframe>
</body>
</html>
a.html
<html>
<head>
<title>
</title>
<script>
</script>
</head>
<body>
<h3> Frame</h3>
<form id="myform">
Req value <input type="text" name="a" id="a">
</form>
</body>
</html>
when i call sendvalue function then the value should pass to iframe link page in form for input field. how this can be achieved.
Share Improve this question edited Apr 11, 2017 at 11:11 vijay kumar asked Apr 11, 2017 at 11:02 vijay kumarvijay kumar 3773 gold badges12 silver badges31 bronze badges 3- Possible duplicate of Pass value to iframe from a window – MD. Sahib Bin Mahboob Commented Apr 11, 2017 at 11:09
- Possible duplicate of stackoverflow./questions/935127/… – Mattia Galati Commented Apr 11, 2017 at 11:10
- alfilatov./posts/… – Alex Filatov Commented Nov 7, 2020 at 5:13
3 Answers
Reset to default 3You can use postMessage
in order to municate between 2 iframe.
On the parent frame you need to grab the iframe's window with iframeEl.contentWindow.postMessage(value as a string, can be json.stringified value)
On the child frame you need to add event of type message
, something like that:
window.addEventListener('message', function(dataPassed) {
//Your logic here.
})
For more info read that
<html>
<head>
<title>
</title>
<script>
function sendvalue(v){
document.getElementById('va').value = v;
window.frames['framename'].document.getElementById("a").value = v
}
</script>
</head>
<body>
<form id="f">
<input type="button" value="123" onclick="sendvalue(this.value)">
<input type="text" name="va" id="va">
</form>
<iframe name="framename" src="a.html" width="500px;"></iframe>
</body>
</html>
a.html
<html>
<head>
<title>
</title>
</head>
<body>
<h3> Frame</h3>
<form id="myform">
Req value <input type="text" name="a" id="a">
</form>
</body>
</html>
The Best way it to add to you Iframe
<iframe id="frame1" src="a.html?data=SomeData" width="500px;"></iframe>
and inside the iframe you can get the information by making varible
var MYDATA = location.href.split('=')[1].split('&');
(you will see that MYDATA=SomeData)
本文标签: How to pass value from parent to its iframe using javascriptStack Overflow
版权声明:本文标题:How to pass value from parent to its iframe using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744251199a2597257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论