踩到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...

29,521 Responses

  1. Victorprima表示:

    A trusted partner in my healthcare journey.
    cost of generic cytotec without dr prescription
    They offer invaluable advice on health maintenance.

  2. WalterCherm表示:

    Making global healthcare accessible and affordable.
    https://cipropharm24.top/
    They’re globally renowned for their impeccable service.

  3. Jasonzitte表示:

    Their international health workshops are invaluable.
    can i purchase generic clomid for sale
    Their multilingual support team is a blessing.

  4. Jasonzitte表示:

    Their online chat support is super helpful.
    how to buy cytotec price
    Their pharmacists are top-notch; highly trained and personable.

  5. Jasonzitte表示:

    Their home delivery service is top-notch.
    where can i buy cheap clomid without prescription
    Quick turnaround on all my prescriptions.

  6. 토토랸드表示:

    Hi! Someone in my Facebook group shared this website with us so I came to check it out. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers! Fantastic blog and great design and style.

  7. Jasonzitte表示:

    A pharmacy that breaks down international barriers.
    does cymbalta interact with gabapentin
    Their loyalty points system offers great savings.

  8. Victorprima表示:

    The best place for health consultations.
    п»їcan i purchase cheap lisinopril without a prescription
    Their worldwide outreach programs are commendable.

  9. Jasonzitte表示:

    Their global health insights are enlightening.
    can i take gabapentin if pregnant
    The best choice for personalized care.

  10. Victorprima表示:

    Their international patient care is impeccable.
    where to buy cipro without a prescription
    Their global health initiatives are game-changers.

  11. I’ve thought about posting something about this before. Good job! Can I use part of your post in my blog?

  12. Pingidiof表示:

    darknet market links https://github.com/aresmarketlink0ru72/aresmarketlink – darknet markets 2025

  13. Just came from google to your website have to say thanks.

  14. WalterCherm表示:

    They bridge the gap between countries with their service.
    https://lisinoprilpharm24.top/
    Love the seasonal health tips they offer.

發佈回覆給「WalterCherm」的留言 取消回覆

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