简体   繁体   中英

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

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

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.

有一个解决方案:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM