14 February 2011

Reading IFrame Contents

Consider an aspx page with IFrame as
   1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageWithIFrame.aspx.cs" Inherits="PageWithIFrame" %>
   2: <html>
   3: <body>
   4:     <form id="form1" runat="server">
   5:     <div> <asp:TextBox runat="server" ID="TxtName"></asp:TextBox>
   6:     </div>
   7:     </form>
   8: </body>
   9: </html>

Page which contain IFrame
1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageThatUsesIFrame.aspx.cs" Inherits="PageThatUsesIFrame" %> <html>
   2:   <head>
   3:       <script src="Script/jquery.js" type="text/javascript"></script>
   4:   </head>
   5:   <body>
   6:       <form id="form1" runat="server">
   7:       <div>
   8:           <iframe id="upfMyFrame" scrolling="no" frameborder="0" style="border-style: none; margin: 0px; width: 100%; height: 40px;" src="PageWithIframe.aspx"></iframe>    
   9:           <asp:Button runat="server" ID="btnSet"  Text="Set Textbox Value" />
  10:       </div>
  11:       </form>
  12:   </body>
  13:   </html>

Now the Jquery code to read the content
1: <script language="javascript">
   2:      $(document).ready
   3:      (
   4:         function() 
   5:         {
   6:             $("#btnSet").click
   7:             (
   8:                  function() 
   9:                  {
  10:                      alert($('#upfMyFrame').contents().find("body #txtName").val());
  11:                      return false;
  12:                 }
  13:            );
  14:        }
  15:      );
  16:   </script>
  17:  

3 comments:

  1. U can not access different domain iframe content with above jquery code because it gives error -
    permission denied to access property 'nodetype'

    ReplyDelete
  2. anybody know solution for this issue - permission denied to access property 'nodetype'?

    ReplyDelete

Your comments, Feedbacks and Suggestions are very much valuable to me :)

Things are upgraded

My Dear readers, I am really thankful for being supportive all these years. This site was the first blog site I ever created in my life...