...

View Full Version : Iframe getElementsByTagName() Works on Local File but not External



gjenkins
02-08-2006, 04:20 PM
I am new to DOM scripting, and I can not find documentation on this ... The following script never completes the getElementsByTagName("A") call if the "content" frame is loaded with a live site like www.google.com or www.dictionary.com, etc ... IF I take the same sites, and copy the source to a local file and then use that file in the frame, then the call works fine.

===== Page Follows =====>

<html><head><script type="text/javascript">

function filter()
{
var output = document.getElementById("output").contentDocument;
if( output ) { output.write( "output found<BR>\n" ); }

var content = document.getElementById("content").contentDocument;
if( content ) { output.write( "content found<BR>\n" ); }

var obj = content.getElementsByTagName("A");

output.write( "Tags Found!<BR>" );

for( var i=0;i<obj.length;i++)
{
output.write( "_<BR>" );
}

output.write( "Tags Iterated!<BR>" );
return 0;
}

</script></head><body>

<a onclick="filter();">filter</a>

<div id="out">
<iframe name="output" id="output" frameborder="2" height=100 width=100% scrolling=auto>
</iframe>
</div>

<div id="cntnt">
<iframe name="content" id="content" frameborder="0" src="http://www.google.com/" height=100% width=100% scrolling=auto>
</iframe>
</div>

</body></html>

liorean
02-08-2006, 05:15 PM
It's the security model of JavaScript that does that - it only gives access to documents originating from the same domain. This is called the Same Origin Policy.

gjenkins
02-08-2006, 05:23 PM
I see, and thank you! Ultimately, I can get the script inlined into pages on the site I am concerned with, but I have a couple o questions ...

1. Can it be subverted? In this case to implement a generic filter on external pages.

2. If I host a script on a remote server, but include it in files on the server in question, will that work?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum