Phillip YS
Phillip YS

Reputation: 904

How can I get document from iframe from different origin?

I'm trying to get document from iframe using

document.getElementById('product-page').contentWindow.document

but I keep getting this error below, is there any way i can avoid the error?

VM1342:1 Uncaught DOMException: Blocked a frame with origin "URL" from accessing a cross-origin frame. at :1:54

Upvotes: 1

Views: 2925

Answers (2)

Ajanth
Ajanth

Reputation: 2515

This is a restriction due to Same Origin Security Policy, If the iframe is from a different domain/port/protocol, you cannot access it via javascript.

Origin is considered different if at least one of the following parts of the address is not the same as your calling url:

<protocol>://<hostname>:<port>/path/to/page.html

But there is a way if you own both the websites you're trying to access,

Refer this answer for more info : SecurityError: Blocked a frame with origin from accessing a cross-origin frame

Upvotes: 1

Adri Aulia Rakhman
Adri Aulia Rakhman

Reputation: 79

maybe its cannot because diffrent origin http

if diffrent hostname and port each iframe will be get CORS

For an explanation of CORS, please see this answer from programmers: https://softwareengineering.stackexchange.com/a/253043/139479

Upvotes: 1

Related Questions