Is there a way to prevent an iframe from redirecting parent window, but in such a way that “top level” redirects still work inside the iframe itself?

后端 未结 2 1372
情歌与酒
情歌与酒 2021-02-12 15:41

So I\'ve read about the HTML5 sandbox property and I understand that if I want to prevent an iframe redirect its parent window I can use the sandbox property leavin

相关标签:
2条回答
  • 2021-02-12 15:52

    With HTML5 the iframe sandbox attribute was added.

    At the time of writing this works on Chrome, Safari, Firefox and recent versions of IE and Opera but does pretty much what you want:

    Allows the iframe content to be treated as being from the same origin as the containing document

    <iframe src="url" sandbox="allow-same-origin"></iframe>
    

    Browser Compatibility enter image description here


    Some Useful links

    • w3schools for sandbox
    • developer.mozilla.org iframe
    • -
    0 讨论(0)
  • 2021-02-12 16:05

    You can use the onbeforeunload property and determine if you wan to redirect or not.

    Here is the docs page for it

    Basically what I would try is this:

    • Make a function that adds the sandbox attribute with everything, just leaving out the allow-top-navigation, to the iframe
    • Bind a function to the onbeforeunload property of the iframe that calls the function that adds the sandbox attribute (be sure not to return anything because a dialog will pop-up)

    This should work because the request is made in the iframe first, and then we can prevent it from carrying over to our top level window.

    Another thing you should check is if you maybe left out the allow-formsoption, which can cause what you are describing.

    Please let me know if any of this worked.

    0 讨论(0)
提交回复
热议问题