Jump to content

iframe tbody row count?


A JM

Recommended Posts

Can someone help with the syntax on getting the number of rows in an iframe tbody?

 

I think this is giving me the table rows not the tbody rows, not sure though...

 

    var frame = document.getElementById("jupload");
    var rows = frame.contentDocument.forms[0].getElementsByTagName("table")[0].rows.length;

 

 

I think it should be closer to this:

 

    var frame = document.getElementById("jupload");
    var rows = frame.contentDocument.forms[0].getElementsByTagName('table')[0].getElementsByTagName("tr").length;

Link to comment
Share on other sites

I'm also trying another variation without much luck..

 

    var frame = document.getElementById("jupload");
    var tbl = frame.contentDocument.forms[0].getElementById("table");
    var rows = tbl.tbodies[0].getElementsByTagName('tr').length;
    alert(rows);

Link to comment
Share on other sites

It won't work if the IFrame's domain, protocol and port do not exactly match the domain, protocol and port of the site which is currently executing the script.

The keyword is Same-Origin Policy to prevent malicious attacks on websites that are not your own.

 

If the IFrame does match your domain, protocol and port, you need to access the contents of the IFrame, you can do that with the .value property (or with the jQuery .contents method).

Link to comment
Share on other sites

As long as the hostnames match, the SOP is out of effect. Your code has that, for example, but an iframe from subdomain.domain.com would be detected as not from the same origin as code from othersub.domain.com or even from domain.com.

 

Like I said, the IFrame you have is from the same origin, so you can access the IFrame with jQuery("iframe").contents(), for example.

Link to comment
Share on other sites

Using the following, I'm able to retrieve the frame.name property and frm.name property but I am unable to get the tbl.id?? So, I'm missing something obvious.. maybe how the object is being referenced??

 

    var frame = document.getElementById("jupload");
    var frm = frame.contentDocument.getElementById("fileupload");
    var tbl = frm.getElementsById("theTable");
    alert(tbl.id);

 

I think that since I'm able to get the table and form on the iFRams that also answers the question about the Origin.

 

Thanks for the help.

Link to comment
Share on other sites

Still not  having any luck. I'm able to get the iFrame name but not any of the objects on the iFrame, specifically the table and count of <tr>?

 

I get the iFrame name here:

 

   var frame = document.getElementById("jupload");

   var frm = frame.contentDocument.getElementById("fileupload");

   alert(frm.name);

 

But I can't get the Table ID here??

 

    var frame = document.getElementById("jupload");
    var frm = frame.contentDocument.getElementById("fileupload");
    var tbl = frm.contentDocument.getElementsById('theTable');

    alert(tbl.id);

 

Its erroring on the line: 

 

var tbl = frm.contentDocument.getElementsById('theTable');

 

Because if I replace alert(tbl.id); with alert("test"); I get no alert...

 

    var frame = document.getElementById("jupload");
    var frm = frame.contentDocument.getElementById("fileupload");
    var tbl = frm.contentDocument.getElementsById('theTable');

    alert("test");

Edited by A JM
Link to comment
Share on other sites

Still not  having any luck. I'm able to get the iFrame name but not any of the objects on the iFrame, specifically the table and count of <tr>?

 

I get the iFrame name here:

 

   var frame = document.getElementById("jupload");

   var frm = frame.contentDocument.getElementById("fileupload");

   alert(frm.name);

 

But I can't get the Table ID here??

 

    var frame = document.getElementById("jupload");

    var frm = frame.contentDocument.getElementById("fileupload");

    var tbl = frm.contentDocument.getElementsById('theTable');

    alert(tbl.id);

 

Its erroring on the line: 

 

var tbl = frm.contentDocument.getElementsById('theTable');

 

Because if I replace alert(tbl.id); with alert("test"); I get no alert...

 

    var frame = document.getElementById("jupload");

    var frm = frame.contentDocument.getElementById("fileupload");

    var tbl = frm.contentDocument.getElementsById('theTable');

    alert("test");

 

Check your line just after var frm = ...

 

Your var frm has no property called contentDocument (unless it is an iframe within an iframe...), just call frm.getElementById("theTable") (you are still using a method called getElementsById() which does not exist... I highlighted the error for you).

Link to comment
Share on other sites

So, I finally got it working.. thanks to your help.

 

Helping me with what I was doing wrong was enough to get me over the hump..

        var iframe = document.getElementsByTagName('iframe')[0];
        var tbl = iframe.contentWindow.document.getElementById("theTable");
        var tbdy = tbl.getElementsByTagName('tbody')[0].getElementsByTagName('tr').length;

AJM,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.