Forum Moderators: open

Message Too Old, No Replies

Finding a node

         

Nautilus

8:01 pm on Dec 16, 2010 (gmt 0)

10+ Year Member



I am trying to find the textContent for a HTML page called by an iframe SRC but cannot come up with the correct code to find it. Firebug shows its there nested under parent and child but its too confusing to make sense of what code will get me there. DOM structure is :

html >> body >> iframe >> html >>body >>textContent

using window.document.body.textContent; returns the textContent of the iframe but I cannot figure out the code to get me to the last lower level textContent where the body content of the iframe SRC HTML page resides.

Hope someone can help !

JAB Creations

1:35 am on Dec 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You didn't post any code though I do know offhand that IE handles text nodes differently than standards compliant browsers do.

- John

Nautilus

9:31 am on Jan 29, 2011 (gmt 0)

10+ Year Member



At first I had a problem getting at the iFrame content and thought it was a node issue (see original post). This turned out to be some quirk where I was only defining an iFrame ID and not both iFrame ID and iFrame Name attributes. Apparently using document.getElementById('iFrame'), even though its getting the ID element, if the iFrame Name attribute is not defined it causes the iFrame to fail with nothing. Stupid but there it is ....

Using this I am trying to build a JS search for my website where site pages are loaded one at a time into a hidden iFrame, the Bodies extracted and searched, then matching results returned as page links. I have this all worked out except for one problem that persists. If an HTML page that is loaded into the iFrame does not exist (ie. Page Not Found) it causes the end of the script, no error, no nothing. I have traced this to a single line of code where the error happens.

var iFrameBody=window.frames['iFrame'].document.body.innerHTML;
alert(iFrameBody);

If the HTML page exists everything works fine and I get the alert containing the Body of the HTML page. However if the HTML page does not exist (ie. Page Not Found) then the script stops responding at the first line and I get no alert. This halts the script in its loop from loading any remaining HTML pages I have defined in an array.

What is confusing is if I unhide the iFrame so I can see the HTML pages loading in it. I can see the Page Not Found when it loads into the iFrame but there seems to be no document.body.innerHTML .... If I test with

var iFrameBody=window.frames['iFrame'] ... The result is [object Window]

Adding .document after this causes the script to fail at this point. I have tried dozens of ways to try to detect some inner content of the Page Not Found. If I can see something in the iFrame saying Page Not Found, in theory that is some inner content I should be able to access for detection purposes.

Any thoughts ?

daveVk

10:19 am on Jan 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will not be able to read the frame content if the same origin policy is violated, perhaps the 404 is considered a violation ?

rainborick

4:24 pm on Jan 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Be sure to test:

if (window.frames['iFrame'].document.body.innerHTML != null)

before you try to assign your variable iFrameBody. Accessing an undefined variable will cause IE to stop executing the script.

Nautilus

11:07 pm on Jan 29, 2011 (gmt 0)

10+ Year Member



Thank you for the responses !

rainborick, I tried the test code you suggested and I am assuming that I would finish your IF code with curly braces and an alert inside so I know something has happened. So I tried adding your code before var iFrameBody is defined but the script still stopped responding at that point in the script. No alerts on if or else and no browser error either.

As far as I can determine when the iFrame SRC is a page that does not exist the innerHTML and Body and Document do not exist. I say this because in order to get a response I have to remove the code all the way back to ...
window.frames['iFrame'] in which case I get an alert that says [object Window] but the minute I add Document for example ...window.frames['iFrame'].Document the script fails at that point.

daveVk, Right now I have only run this script in testing on my local computer with the script and all HTML pages inside the same directory on my local computer. In the end I want to this run script on my web server to search HTML pages in different directories all on my web server. I wouldn't think in any case that origin or anything external would be an issue. Am I not the owner of the ip, host name, and files in both cases ? I am not saying I know this for sure though !

My one thought was that the Page Not Found maybe something specific to the browser so I would need to find code to access some browser property to detect the file does not exist. I'm using FF 3.6.1.3 right now for my initial tests and will rewrite for other browsers eventually. I just can't get over the fact that I see the Page Not Found load in the iFrame but can't get access to anything that could be used for detection. Since I can get a response from the [object Window] as window.frames['iFrame'] there seems like there should be some code that will access something underneath that node which I can use to detect a difference in a page that loads successfully. I have yet to stumble on it though and have turned to this forum for more experienced feedback. Thanks all !

Just to add ... I know I could solve this problem by adding a PHP/Perl script that detects a failed page load before running the JS script but I don't want to do that and shouldn't think I have to considering I have owner permission of everything involved here. If that ends up being the case that negates the simplicity of using JS and I might as well write the whole thing in PHP/Perl, forget about JS, and and be done with it. Anyone disagree ?

daveVk

1:19 pm on Jan 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried putting the failing code in a try/catch block ? It seems a bit odd that there is no js error being reported, I assume you have error reporting on.

Nautilus

11:26 pm on Jan 30, 2011 (gmt 0)

10+ Year Member



I am not familiar with try/catch. Is that Java applet code and not Javascript code ? Could you provide an example to Try ....

window.frames['iFrame'].document.body.innerHTML;

FF has an error console which is throwing the following error with regard to the Page Not Found.

Error: Permission denied for <file://> to get property Window.document from <moz-safe-about:neterror?e=fileNotFound&u=file%3A///E%3A/testdir/test1.html&c=ISO-8859-1&d=Firefox%20can%27t%20find%20the%20file%20at%20/E%3A/testdir/test1.html.>.
Source File: file:///E:/testdir/search.html
Line: 23


Sound like you were right about the Page Not Found being denied permission although this still doesn't make sense to me as everything is running locally. Could the try/catch work on the above error ?

daveVk

11:55 pm on Jan 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try
{
//Run some code here
}
catch(err)
{
//Handle errors here
}

[w3schools.com...]

The security features seem to be a bit of a kludge, hope try/catch works.

Nautilus

5:13 pm on Feb 2, 2011 (gmt 0)

10+ Year Member



daveVK,

The try/catch worked like a charm to trap the Page Not Found. Thank you so much for the advice !