Sweet Alert 2 Open in Parent Frame

北慕城南 提交于 2019-12-09 20:38:02

问题


I have an iframe with a source that is a php file. What I'm trying to do is get Sweet Alert 2 to open in the parent frame rather than inside of the iframe. I have tried changing the target and have not had any success.

None of the following target options have worked:

swal({
    target: 'window'
    target: 'document'
    target: 'parent'
});

This is the result that I get when 'body' is the target:

Edit:

SweetAlert 2 code

swal({
    type: 'error',
    text: 'You did not select any files.',
    target: 'top'
});

iFrame code

<div id="viewWindow" class="col-xs-10 content">
    <iframe id="waiverList" src="php/edit_archive.php"></iframe>
</div>

回答1:


Hi what you can do is have your sweetalert2 code in your parent window. Then use JavaScript's parent property inside your iframe to call the function. To avoid getting a same origin policy error you can use window post messages.

like this:

Parent Html

<!DOCTYPE html>
<html>
<body>
<script> 
    function openAlert() {
        swal(
            'Good job!',
            'You clicked the button!',
            'success'
        )
    }
    window.addEventListener("message", function(event) {
        if(event.data == "openAlert");{
            openAlert();
        }
    });

</script>
    <iframe src="iframe.html">

    </iframe>
</body>
</html>

Iframe HTML

<!DOCTYPE html>
<html>
<body>
     <button onclick="foo();">Click Me</button>
     <script>
     function foo(){
         parent.postMessage("openAlert", "*");
     }
     </script>
</body>
</html>


来源:https://stackoverflow.com/questions/47381063/sweet-alert-2-open-in-parent-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!