Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing iframe url changes parent window url Blocked a frame with origin

I use this code

<script>
  <!-- Hide Script
    if (top.location != self.location) {
     top.location = self.location
    }
  //End Hide Script -->
</script>

However recently I get such errors in the chrome console and the code stopped working

(index):15 Unsafe JavaScript attempt to initiate navigation for frame with origin 'http://preview.themeforest.net' from frame with URL 'http://site.test'. The frame attempting navigation is targeting its top-level window, but is neither same-origin with its target nor has it received a user gesture. See https://www.chromestatus.com/features/5851021045661696.

(index):21 Uncaught DOMException: Failed to execute 'replace' on 'Location': The current window does not have permission to navigate the target frame to 'http://site.test''.

In Firefox and Opera this code works. In Chrome it does not work.

How to fix these errors?

It's a bit like this question

like image 642
Виктор Лернер Avatar asked Aug 22 '18 08:08

Виктор Лернер


1 Answers

Please check the same origin policy: https://en.wikipedia.org/wiki/Same-origin_policy

The reason for that error is that you violate that policy.

The only way I know to avoid this is to setup the iframe sandboxed (kinda restricted) with the allow-top-navigation flag.

<iframe src="demo_iframe_sandbox.htm" sandbox="allow-top-navigation"></iframe>

Source: https://www.w3schools.com/tags/att_iframe_sandbox.asp

Check the restrictions and see if that solution fits for you.

like image 80
epanalepsis Avatar answered Sep 19 '22 02:09

epanalepsis