4

everyone. I created my page and inserted inside another page using IFrame. Now I'm trying to get whole text of that inserted page using pure javascript or jquery. Here's what I have now, but it doesn't work...

    <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.5.1.js"></script>   
<script type="text/javascript">
function getFrameContents(){
   return $("#myframe").contents().find('body');
}
</script>
</head>

<form>
<input type="button" value="CodingForums" onClick="window.alert(getFrameContents())" />
</form>

<iframe id='myframe' src="http://www.fxstreet.com/rates-charts/currency-rates/" width="100%" height="300">
  <p>Your browser does not support iframes.</p>
</iframe>

</html>

I've read also that there is some policy about accessing pages from different server. How can I do it properly?

Konstantin Milyutin
  • 11,946
  • 11
  • 59
  • 85

2 Answers2

4

I've read also that there is some policy about accessing pages from different server.

Yes there is and the policy says it's strictly forbidden.

How can I do it properly?

Fetch it on the server-side. You might want to use a server-side cache too.

vbence
  • 20,084
  • 9
  • 69
  • 118
  • Why is it forbidden? I just want to read content of the loaded page. I can do it with broswer, why I can't do it with js? – Konstantin Milyutin Mar 08 '11 at 13:15
  • because you can then inject the page, which means you can redirect it to whereever you wish, hijack it :) – Val Mar 08 '11 at 13:46
  • @damluar There is a class of attacks called XSS (Cross-site scripting) when the attaker can execute script in a page's content. This way they can access cookies, session data etc. - Just imagine, you open gmail.com in a hidden iframe and read the visitors' emails. – vbence Mar 08 '11 at 14:01
  • @vbence Ok, agree. can I access this data if I write firefox plugin? here is a description of what I want: http://stackoverflow.com/questions/5233397/get-content-of-page-including-iframe-from-firefox-plugin – Konstantin Milyutin Mar 08 '11 at 14:04
  • 1
    @damluar Feel free to vote up or even accept answers if you think they were satisfying. - And yes, you can do anything from a firefox plugin (legal questions aside). – vbence Mar 08 '11 at 14:25
1

Many people ask this question...

The best way people describe is (as i dont like iframes) but you need on iframe to collect the data and use window.top.... to access the main page and pass it that way because .contents() is not supported by all browsers.

Val
  • 17,336
  • 23
  • 95
  • 144