0

I'm inserting an iframe to display another website's content in mine, and I'd like to clean it up first. So I tried to use $('iframe').contents, but the return is undefined, while $('iframe') works like a charm. Is there some kind of cross site security here ?

Bonus question : how should I clean the page then ? I just need div#main, not the rest. I'm using an iframe so I can get the original styling.

Thanks for your time

ksol
  • 11,835
  • 5
  • 37
  • 64
  • 1
    Refer this [http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe](http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe) – Siva Charan Feb 20 '12 at 17:04

2 Answers2

4

Is there some kind of cross site security here?

Yes, the Same Origin Policy.

There aren't many ways around it - I once used PHP to fetch the contents of the target webpage and write them locally, but that was quite a specific thing (and I owned both domains).

Take a look at this question: jQuery cross domain iframe scripting

Community
  • 1
  • 1
Grim...
  • 16,518
  • 7
  • 45
  • 61
  • Getting the content of the target page is actually not an issue and very easy to do with ruby/nokogiri. I try not to use iframes, but in this case, it was useful in order to have a nice rendering without putting effort in it. Anyway, thanks to you & Linus below. – ksol Feb 20 '12 at 17:44
2

It's called Same-Origin Policy. Basically, you can't use javascript to interact with pages from another domain (protocol, domain and port must all match).

Linus Thiel
  • 38,647
  • 9
  • 109
  • 104