踩到Window resize在Preview print的雷

最近在作案的過程中遇到一個狀況,從Art design那邊拿到的頁面,會在Window resize的時候執行某個Function,本來一切都正常,但測試到Chrome和Firefox預覽列印時發現也會Trigger這個Event,會讓在預覽列印過後的頁面發生問題,所以必須要進行修正,以下是這個案例的說明:

先在測試頁加入以下的程式,發現在預覽列印的時候會Trigger window resize event:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Preview Print Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        $(window).resize(function() {
            console.log("Window Resize");
        });
    </script>
</head>
<body>
<h1>Window Resize Test</h1>
</body>
</html>
Preview print trigger window resize event
Preview print trigger window resize event

網路上搜尋了一下解決方案,將Script改寫如下,可以順利解決,但不知道是不是最好的方法,如果大家有更好的也別忘了告訴我唷~

openResizeFn();

// Chorme
var print = false;
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function (mql) {
    if (mql.matches) {
        closeResizeFn();
    } else {
        setTimeout(openResizeFn, 2000);
    }
});

// Firefox
window.onbeforeprint = function () {
    closeResizeFn();
};
window.onafterprint = function () {
    setTimeout(openResizeFn, 2000);
};

// Common Function
function openResizeFn() {
    $(window).on("resize", windowResize);
}

function closeResizeFn() {
    $(window).off("resize", windowResize);
}

function windowResize() {
    console.log("Window Resize");
}

You may also like...

16,583 Responses

  1. Alita Barno表示:

    The unique viewpoints in The writing never fail to impress me. Insightful as always!

  2. Jeffreywex表示:

    http://gramster.ru/# пин ап зеркало

  3. RobertCow表示:

    pinup 2025: Gramster – пин ап зеркало

  4. DarrenWab表示:

    пин ап зеркало gramster pinup 2025

  5. WilliamDen表示:

    https://gramster.ru/# пинап казино

  6. DarrenWab表示:

    пин ап казино gramster pinup 2025

  7. Jamesboype表示:

    https://gramster.ru/# пин ап зеркало

  8. bokep viral表示:

    Howdy I wanted to write a new remark on this page for you to be able to tell you just how much i actually Enjoyed reading this read. I have to run off to work but want to leave ya a simple comment. I saved you So will be returning following work in order to go through more of yer quality posts. Keep up the good work.

  9. Reading The work is like catching up with an old friend; comfortable, enlightening, and always welcome.

  10. Donn Chesher表示:

    The Writing is like a lighthouse for my curiosity, guiding me through the fog of information.

  11. RobertCow表示:

    pinup 2025: Gramster – gramster.ru

  12. myspace.com [url=https://bookmarkstore.download/story.php?title=for-more-information-kindly-visit-xxxbp-tv-5#discuss]https://bookmarkstore.download/story.php?title=for-more-information-kindly-visit-xxxbp-tv-5#discuss[/url] .

  13. Jamesboype表示:

    http://gramster.ru/# пин ап казино зеркало

  14. RogerAnove表示:

    продажа электрокаминов в москве – встроенный биокамин, купить электрокамин с порталом

  15. Jeffreywex表示:

    https://gramster.ru/# pinup 2025

  16. WilliamDen表示:

    http://gramster.ru/# пин ап

  17. WilliamDen表示:

    https://gramster.ru/# пин ап казино официальный сайт

  18. Jamesboype表示:

    http://gramster.ru/# gramster.ru

  19. Jeffreywex表示:

    https://gramster.ru/# pinup 2025

  20. Brilliant writing! You’ve captured the essence perfectly, much like a photographer captures a stunning landscape.

  21. Myles Aikey表示:

    Discovering The Writing has been a game-changer for me. The contributions are invaluable.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。