A JM Posted November 21, 2013 Share Posted November 21, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/ Share on other sites More sharing options...
A JM Posted November 21, 2013 Author Share Posted November 21, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459315 Share on other sites More sharing options...
Irate Posted November 21, 2013 Share Posted November 21, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459323 Share on other sites More sharing options...
A JM Posted November 21, 2013 Author Share Posted November 21, 2013 Could you be a little more specific as to what you mean by the same domain? the parent url: http://mydomain.com/dbpages/index.html the iFrame url:http://mydomain.com/jquery/iframe.html So what should that cocde look like if both of those cases are true? Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459346 Share on other sites More sharing options...
Irate Posted November 21, 2013 Share Posted November 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459360 Share on other sites More sharing options...
A JM Posted November 21, 2013 Author Share Posted November 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459414 Share on other sites More sharing options...
kicken Posted November 21, 2013 Share Posted November 21, 2013 There is no such method getElementsById. If you are trying to get a table with id="theTable" then just do frame.contentDocument.getElementById('theTable'). Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459430 Share on other sites More sharing options...
A JM Posted November 22, 2013 Author Share Posted November 22, 2013 (edited) 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 November 22, 2013 by A JM Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459503 Share on other sites More sharing options...
Irate Posted November 22, 2013 Share Posted November 22, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459511 Share on other sites More sharing options...
A JM Posted November 22, 2013 Author Share Posted November 22, 2013 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, Quote Link to comment https://forums.phpfreaks.com/topic/284126-iframe-tbody-row-count/#findComment-1459562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.