[英]How can I pass value to iFrame with javascript?
I opened an iFrame in my page and onClick event try to send a value to the textbox of the iframe 我在页面中打开了一个iFrame,onClick事件尝试将值发送到iframe的文本框
This is my javascript code 这是我的JavaScript代码
<script type="text/javascript">
function TextToFrame()
{
var frame = frames['frame1'];
frame.document.getElementById("u").value = "wallace";
}
</script>
IFrame loads the page but when I click the Button , it doesn't send any value. IFrame会加载页面,但是当我单击Button时,它不会发送任何值。 It refreshes the main page and iFrame 刷新主页和iFrame
<asp:Button ID="Button1" runat="server" OnClientClick="TextToFrame();" Text="Send Value" />
<IFRAME id="frame1" name="frame1" src="http://tweakers.net/my.tnet/login?location=http%3A%2F%2Ftweakers.net%2F" width=500px height=500px runat="server">
What am I doing wrong? 我究竟做错了什么? any help appreciated 任何帮助表示赞赏
I don't think you can modify the HTML in an iframe from a different domain. 我认为您无法在其他域的iframe中修改HTML。 You might be able to change the iframe source to something like: 您可能可以将iframe源更改为以下内容:
http://tweakers.net/my.tnet/login?u=wallace
but the site should give you an option like that. 但该网站应为您提供类似的选择。
有一个解决方案:
document.getElementsByName('frame1')[0].contentWindow.getElementById("u").value = "wallace";
您无法在src
属性与其所在域不同的iframe中操纵数据。
It won't allow you on cross-domain. 它不允许您跨域使用。
Have a look at this - Same origin policy 看看这个- 同源政策
I agree with Haroldis but one more thing: 我同意Haroldis的观点,但还有一点:
<script type="text/javascript">
function TextToFrame()
{
document.getElementsByName('frame1')[0].contentWindow.getElementById("u").value = "wallace";
return false; // For No Post Back in the container Page
}
</script>
and the button: 和按钮:
<asp:Button ID="Button1" runat="server" OnClientClick="return TextToFrame();" Text="Send Value" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.