jQuery $(window).unload() 尝试关闭窗口时失败

我正在用较小设备的全屏覆盖替换模型窗口。假设 $(window).hide() 不必要地将该窗口保留在内存中,我正在尝试使用 $(window).unload(),但它没有按预期工作。我使用一个 PHP 变量作为脚本名称,因为不同的脚本将使用此实用程序。


在进一步探索时,我发现 unload() 已被弃用,并且可能一开始并不打算以这种方式使用。除了 $(window).hide 之外,我不知道还有什么选择。


这是完整的代码


<?php

    session_start();

    $Script = $_REQUEST[Script];

?>

<!DOCTYPE html>

<html>

    <head>

        <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>

        <style type="text/css">

            body {width:100%; padding:0; margin:0; background-color:#FFFFFF;}

            .Leave {position:absolute; top:20px; right:20px;}

        </style>

    </head>

    <body>

        <img class="Leave" src="images/CloseButton2.png" alt="close">

        <div id="ScreenOverlay"></div>

        <script>

            $(document).ready(function()

            {

                $("#ScreenOverlay").load("<?PHP echo($Script); ?>", function(response, status, xhr)

                {

                    if(status == "error")

                    {

                        console.log(msg + xhr.status + " " + xhr.statusText);

                    }

                });

                $(".Leave").click(function()

                {

                    $(window).unload();

                });

            });

        </script>

    </body>

</html>


慕桂英546537
浏览 234回答 1
1回答

哆啦的时光机

预期的行为是什么?您不会通过调用卸载从内存中卸载窗口。卸载需要一个事件处理程序也许你想要$(".Leave").click(function()&nbsp;&nbsp;$(window).close()&nbsp;});假设你自己从同源脚本打开了窗口没有 window.hide 这样的东西如果您只想卸载的内容,$("#ScreenOverlay")则执行$("#ScreenOverlay").empty()或$("#ScreenOverlay").remove()
打开App,查看更多内容
随时随地看视频慕课网APP