1

i want to search a simple string like <div class="sender" style="display: block;"> in iframeon a external website.

The code don't work

var target = '<div class="sender" style="display: block;">'
console.log( $( "iframe" ).contents().find('body').html().search( target ) != -1 )

because i have an error

Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://website1.com" from accessing a frame with origin "http://website2.com". Protocols, domains, and ports must match.

How can I do please?

Rod
  • 712
  • 2
  • 12
  • 36
  • 3
    If the iframe is on a different domain, then you *cannot*. It's impossible. – gen_Eric Feb 15 '16 at 16:32
  • Erf. However i don't want to edit the code, only read and find string.. It's always the same answer? – Rod Feb 15 '16 at 16:38
  • Unfortunately, yes. Since it's an "external domain", the browser's security policy comes into play and blocks this. You can "fix" it if you have access to the pages on both domains. – gen_Eric Feb 15 '16 at 16:46

2 Answers2

1

Look, it said Failed to read the 'contentDocument'.

Since the security rule, you can't get(read) anything of the external website that has a different domain, and you can't edit it too.

It said Protocols, domains, and ports must match..

1

I would suggest using <object></object> or <embed></embed> or better yet solution is if you have access to the server page you are accessing enable cross site scripting in your header when sending it to the client. The issue is this Protocols, domains, and ports must match and the only way for it to load this html is to have the server send the appropriate header also take a look at this how to block website from loading in iframe?

Community
  • 1
  • 1
John
  • 114
  • 10