/    Sign up×
Community /Pin to ProfileBookmark

contentDocument

Hello, thanks in advance for taking the time to read this.

I’m trying to count how many TD are on a page contained in an iframe, i’ve tried doing something like this:

[quote]

<html>
<head>
<script>
function go()
{
theFrame=document.getElementById(1)
idocument=theFrame.contentDocument
TDcount=idocument.getElementsByTagName(“td”).length
alert(TDcount)
}
</script>
</head>

<body>
<iframe id=1 height=500 width=500 src=”http://10.45.4.37/doc/contact.html”></iframe>
<input type=’button’ onclick=”go()” value=’test’>
</body>
</html>

[/quote]

Anyways, I know that i’m not using contentDocument properly because it doesn’t work, so I was hoping someone could explain to me how to make this happen. ^_^

Thanks again!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisOct 25.2006 — To get an iframe's document, you have to go through the window array. Give the iframe a valid name (can't start with a number) like "if1":TDCount = if1.document.getElementsByTagName("td").length;This will only work if the document in the iframe is on the same domain as the containing web page (security issues).
Copy linkTweet thisAlerts:
@mishkinauthorOct 25.2006 — So you're saying that this should work:


<html>

<head>

<script>

function go()

{

theFrame=document.getElementById(1)

iWin=theFrame.contentWindow

TDcount=iWin.getElementsByTagName("td").length

alert(TDcount)

}

</script>

</head>

<body>

<iframe id=1 height=500 width=500 src="http://10.45.4.37/doc/contact.html"></iframe>

<input type='button' onclick="go()" value='test'>

</body>

</html>
[/quote]


I tried changing idocument to iWin, and contentDocument to contentWindow. Now when I run the script I get "access denied". I guess that's the domain issue you were referring to. If I were aiming towards making a script that can go through google searches for me and create a list of the items in a different format than it appears on google, how would I access the content of that remote page? (hypothetically of course)
Copy linkTweet thisAlerts:
@gil_davisOct 25.2006 — Sorry, you can't. Basically if you don't have sufficient security rights, you can't play with it at all. ?
Copy linkTweet thisAlerts:
@KorOct 25.2006 — javascript relationship between pages from different domains is not allowed and not possible due to obvious security reasons.

To "read" asincronousely the content of a page from a page in a different domain, you may use AJAX+server-side application or simply a servers ide-application and an include. php and asp are the most frequent languages which can build that kind of server-side application.
Copy linkTweet thisAlerts:
@mishkinauthorOct 25.2006 — Kor, I've heard you mention Ajax before, which to my understanding is a netscape version of active x. Is this an accurate statement, and if so, can you compare it to active x?
Copy linkTweet thisAlerts:
@KorOct 25.2006 — AJAX is in fact a single request object (with it's properties). Well, 2 objects, a XMLHttpRequest object (for DOM compliant browsers) and it's IE variant, an ActiveXObject of a Microsoft.XMLHTTP argument. Both have almost the same properties and methods and both do the same thing: send/receive requests (querries) to an XML file or to a server-side application.

The request comes back as a string (if the request is responseHTML) or as an DOM object (in case of a responseXML asked).

The response now is handled and displayed in page with the help of javascript. The main clue of the AJAX process is that everithing is done asynchronouselly, that means whithin the same session.
Copy linkTweet thisAlerts:
@mishkinauthorOct 25.2006 — Excellent. I'm going to be certainly learning this, thanks.

In regards to the aformentioned issue, since i'm not using a server would it be possible to have ajax read the HTML code from the page after it was downloaded to my temp files? Because that way I should be able to trick it into thinking that it is a local site, or at least have the iframe open the page from temp instead of the url (in real time though, as temp is being updated). Does that sound possible?
Copy linkTweet thisAlerts:
@KorOct 26.2006 — You don't need that. [URL=http://ajaxextended.com/]Extended AJAX[/URL] might do the trick.
×

Success!

Help @mishkin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...