Error blocked a frame with origin from accessing a cross origin frame

I am loading an in my HTML page and trying to access the elements within it using JavaScript, but when I try to execute my code, I get the following error: SecurityError: Blocked a ...

Same-origin policy

You can’t access an <iframe> with different origin using JavaScript, it would be a huge security flaw if you could do it. For the same-origin policy browsers block scripts trying to access a frame with a different origin.

Origin is considered different if at least one of the following parts of the address isn’t maintained:

protocol://hostname:port/...

Protocol, hostname and port must be the same of your domain if you want to access a frame.

NOTE: Internet Explorer is known to not strictly follow this rule, see here for details.

Examples

Here’s what would happen trying to access the following URLs from http://www.example.com/home/index.html

URL                                             RESULT
http://www.example.com/home/other.html       -> Success
http://www.example.com/dir/inner/another.php -> Success
http://www.example.com:80                    -> Success (default port for HTTP)
http://www.example.com:2251                  -> Failure: different port
http://data.example.com/dir/other.html       -> Failure: different hostname
https://www.example.com/home/index.html:80   -> Failure: different protocol
ftp://www.example.com:21                     -> Failure: different protocol & port
https://google.com/search?q=james+bond       -> Failure: different protocol, port & hostname

Workaround

Even though same-origin policy blocks scripts from accessing the content of sites with a different origin, if you own both the pages, you can work around this problem using window.postMessage and its relative message event to send messages between the two pages, like this:

  • In your main page:

    const frame = document.getElementById('your-frame-id');
    frame.contentWindow.postMessage(/*any variable or object here*/, 'https://your-second-site.example');
    

    The second argument to postMessage() can be '*' to indicate no preference about the origin of the destination. A target origin should always be provided when possible, to avoid disclosing the data you send to any other site.

  • In your <iframe> (contained in the main page):

    window.addEventListener('message', event => {
        // IMPORTANT: check the origin of the data!
        if (event.origin === 'https://your-first-site.example') {
            // The data was sent from your site.
            // Data sent with postMessage is stored in event.data:
            console.log(event.data);
        } else {
            // The data was NOT sent from your site!
            // Be careful! Do not use it. This else branch is
            // here just for clarity, you usually shouldn't need it.
            return;
        }
    });
    

This method can be applied in both directions, creating a listener in the main page too, and receiving responses from the frame. The same logic can also be implemented in pop-ups and basically any new window generated by the main page (e.g. using window.open()) as well, without any difference.

Disabling same-origin policy in your browser

There already are some good answers about this topic (I just found them googling), so, for the browsers where this is possible, I’ll link the relative answer. However, please remember that disabling the same-origin policy will only affect your browser. Also, running a browser with same-origin security settings disabled grants any website access to cross-origin resources, so it’s very unsafe and should NEVER be done if you do not know exactly what you are doing (e.g. development purposes).

  • Google Chrome
  • Mozilla Firefox
  • Safari
  • Opera: same as Chrome
  • Microsoft Edge: same as Chrome
  • Brave: same as Chrome
  • Microsoft Edge (old non-Chromium version): not possible
  • Microsoft Internet Explorer
  • Remove From My Forums
  • Question

  • User-1571110992 posted

    Dear Team,

    I create embedded object and used in different domain website. Giving me error . 

    Uncaught DOMException: Blocked a frame with origin "http://a816-dohbesp.nyc.gov" from accessing a cross-origin frame.

    403 (Forbidden ( The server denied the specified Uniform Resource Locator (URL). Contact the server administrator. ))

    Embedded code used at  http://a816-dohmeta.nyc.gov/MetadataLite/newsample.html

    <object  id="emViz_disparities" width="100%" height="700px" data="http://a816-dohbesp.nyc.gov/IndicatorPublic/Embedded/NewDisparities.aspx?id=2046,4466a0,11,Disparities,Rate,years=2005;2010;2014
    
    ,dataLink=Neighborhood%20Poverty"style="overflow: hidden;"></object>

    Embedded page has following code at browser size change. The function name is resizeDis

     window.parent.document.getElementById('emViz_disparities').height = framewidth + 180;

Answers

  • User-707554951 posted

    Hi vishabedre,

    You can’t access an <iframe> with Javascript, it would be a huge security flaw if you could do it. For the same-origin policy browsers block scripts trying to access a frame with a different origin.

    Workaround:

    Even though same-origin policy blocks scripts from accessing the content of sites with a different origin, if you own both the pages, you can work around this problem using window.postMessage and its relative message event to send messages between the two
    pages, like this:

    In your main page:

    var frame = document.getElementById('your-frame-id'); 
    
    frame.contentWindow.postMessage(/*any variable or object here*/, '*'); 

    In your <iframe> (contained in the main page):

    window.addEventListener('message', function(event) { 
    
        // IMPORTANT: Check the origin of the data! 
        if (~event.origin.indexOf('http://yoursite.com')) { 
            // The data has been sent from your site 
    
            // The data sent with postMessage is stored in event.data 
            console.log(event.data); 
        } else { 
            // The data hasn't been sent from your site! 
            // Be careful! Do not use it. 
            return; 
        } 
    }); 

    For more information. You could refer to the following link:

    http://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame

    https://community.microstrategy.com/t5/Web/TN275329-quot-Blocked-a-frame-with-origin-WEBSERVER-from/ta-p/275329

    Best regards

    Cathy

    • Marked as answer by

      Thursday, October 7, 2021 12:00 AM

  • User-707554951 posted

    Hi vishabedre,

    The reason for your problem is that you can’t access an <iframe> with Javascript.

    So, you need to read the workaround in my first reply.

    Best regards

    Cathy

    • Marked as answer by
      Anonymous
      Thursday, October 7, 2021 12:00 AM

  • User-1571110992 posted

    jS fILE
    
    window.addEventListener('message', function (event) {
        if (event.data.viz == "Dis") {
            var data = event.data;
            document.getElementById('emViz_disparities').height = data.height;
        }
    }, false);
    
    Code of Web child page
    
        window.parent.postMessage({
                'height': framewidth + 180,
                'viz': 'Dis',
                'location': window.location.href
            }, "*");
    • Marked as answer by
      Anonymous
      Thursday, October 7, 2021 12:00 AM

when I want to go onclick to next page’s specific div it shows this error .

jquery.js:3123 Uncaught DOMException: Failed to read the ‘contentDocument’ property from ‘HTMLIFrameElement’: Blocked a frame with origin from accessing a cross-origin frame.

and not going on specific div just on page.

EDIT

 <div class="col-sm-12 main-image"><img onclick="document.location='/discovers/styles-services#cat_eye';" src="pub/media/before-after/No/Back.jpg" /></div>

asked Nov 15, 2017 at 8:05

Learner's user avatar

LearnerLearner

8331 gold badge17 silver badges42 bronze badges

This will likely be down to either trying to access an iFrame with Javascript, or you’re trying to load an asset from a different server? I can’t quite tell from your question sorry, it’s not very clear.

You can’t access an <iframe> with Javascript, it would be a huge
security flaw if you could do it. For the same-origin policy browsers
block scripts trying to access a frame with a different origin.

Origin is considered different if at least one of the following parts
of the address isn’t maintained:

<protocol>://<hostname>:<port>/path/to/page.html Protocol, hostname
and port must be the same of your domain, if you want to access a
frame.

Answer taken from this stack exchange post

answered Nov 15, 2017 at 10:57

Ben Crook's user avatar

Ben CrookBen Crook

15.5k3 gold badges51 silver badges99 bronze badges

6

Понравилась статья? Поделить с друзьями:
  • Error bios legacy boot of uefi only media что это значит
  • Error bios legacy boot of uefi only media что делать на старом биосе
  • Error bios legacy boot of uefi only media что делать на старой материнской плате
  • Error bios legacy boot of uefi only media что делать rufus
  • Error bios legacy boot of uefi only media что делать gigabyte