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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With