How can I pass value to iFrame with javascript?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 23:48:44

问题


I opened an iFrame in my page and onClick event try to send a value to the textbox of the iframe

This is my javascript code

<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. It refreshes the main page and 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


回答1:


I don't think you can modify the HTML in an iframe from a different domain. You might be able to change the iframe source to something like:

http://tweakers.net/my.tnet/login?u=wallace

but the site should give you an option like that.




回答2:


There is a solution :

document.getElementsByName('frame1')[0].contentWindow.getElementById("u").value = "wallace";



回答3:


You can't manipulate data inside an iframe which src attribute is different from the domain it's on.




回答4:


It won't allow you on cross-domain.

Have a look at this -Same origin policy




回答5:


I agree with Haroldis but one more thing:

<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" />  


来源:https://stackoverflow.com/questions/5539756/how-can-i-pass-value-to-iframe-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!