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

17,380 Responses

  1. Предлагаем услуги профессиональных инженеров офицальной мастерской.
    Еслли вы искали ремонт телевизоров hisense цены, можете посмотреть на сайте: ремонт телевизоров hisense в москве
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  2. RobertPex表示:

    slot oyunlar? puf noktalar? en kazancl? slot oyunlar? en kazancl? slot oyunlar?

  3. Ангары表示:

    Производство ангаров и складов под ключ.
    Подробная информация и заказ быстровозводимый складов

  4. Justinlib表示:

    http://casinositeleri2025.pro/# en iyi yatД±rД±m siteleri

  5. EdwardApest表示:

    http://slottr.top/# slot oyunlar?

  6. EdwardApest表示:

    http://casinositeleri2025.pro/# vidobet giriЕџ 2025

  7. CesarSep表示:

    Schone naturliiche Augenbrauen bei Microblading Berlin Ihre Anlaufstelle fur Permanent-Make-Up in Berlin Charlottenburg Jetzt Termin vereinbaren!
    https://microblading-berlin.com/ – microblading Berlin

  8. RobertPex表示:

    az parayla cok kazandiran slot oyunlar? en kazancl? slot oyunlar? slot siteleri

  9. TravisFlasp表示:

    deneme bonusu veren gГјvenilir siteler: eski oyunlarД± oynama sitesi – en iyi bahis uygulamasД±

  10. Josephnor表示:

    pinup 2025: пин ап зеркало – пин ап казино официальный сайт

  11. EdwardApest表示:

    http://casinositeleri2025.pro/# bonus veren yasal bahis siteleri

  12. Justinlib表示:

    http://slottr.top/# en kazancl? slot oyunlar?

  13. TravisFlasp表示:

    bonus slot: iddaa siteleri – bet oyun siteleri

  14. Justinlib表示:

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

  15. RobertPex表示:

    en cok kazand?ran slot oyunlar? slot oyunlar? en kazancl? slot oyunlar?

  16. Josephnor表示:

    slot siteleri: slot oyunlar? puf noktalar? – slot tr online

  17. RobertPex表示:

    slot tr online slot oyunlar? slot oyunlar?

  18. Justinlib表示:

    https://pinup2025.com/# pinup 2025

  19. RobertPex表示:

    slot siteleri slot oyunlar? slot oyunlar? puf noktalar?

  20. TravisFlasp表示:

    slot siteleri: en kazancl? slot oyunlar? – slot siteleri

  21. Universal’s Halloween Horror Nights is an annual Halloween-themed event at Common Studios theme parks in Orlando, Hollywood, Japan and Singapore.

  22. Planning a travel journey is difficult, it’s crucial to keep the taste of each and every folks in mind so that no one experiences any drawback within the journey journey.

  23. McWhertor, Michael (February 26, 2020).

  24. 피파대낙表示:

    I am very happy to look your post. Thanks a lot and i am taking a look ahead to touch you.

發佈留言

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