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

21,068 Responses

  1. Briantrozy表示:

    doxycycline 3142: doxycycline 100mg price uk – doxycycline 200 mg pill

  2. Roberttic表示:

    https://amohealthpharm.shop/# order amoxicillin no prescription

  3. VirgilBlawl表示:

    can i purchase clomid without prescription ClmHealthPharm cost of generic clomid price

  4. Briantrozy表示:

    buy generic clomid prices: ClmHealthPharm – order generic clomid without insurance

  5. Roberttic表示:

    https://amohealthpharm.shop/# amoxicillin 500mg capsule

  6. BrandonJag表示:

    buy zithromax 500mg online: zithromax tablets for sale – zithromax 250 price

  7. Roberttic表示:

    http://amohealthpharm.com/# amoxicillin 825 mg

  8. Codigo promocional 1XBET 2025: GRATIS777. Use esse promo code no seu registro e libere ofertas de ate R$ 1200 para esportes e R$ 9500 no cassino. Codigo promocional para una apuesta combinada gratis a futbol compuesta por 3 eventos o mas y con cuotas por evento de 1.8 o superiores.

    codigo promocional 1xbet argentina

  9. Dexikoselmaw表示:

    Codigo promocional 1XBET 2025: GRATIS777. Use esse promo code no seu registro e libere ofertas de ate R$ 1200 para esportes e R$ 9500 no cassino. Codigo promocional para una apuesta combinada gratis a futbol compuesta por 3 eventos o mas y con cuotas por evento de 1.8 o superiores.

    codigo promocional 1xbet argentina

  10. BrandonJag表示:

    cheap clomid without prescription: ClmHealthPharm – where to buy clomid no prescription

  11. BrandonJag表示:

    where to get generic clomid pill: ClmHealthPharm – how can i get cheap clomid price

  12. JustinTam表示:

    doxycycline 500mg tablets: DoxHealthPharm – doxycycline 100mg without a prescription

  13. Only a smiling visitor here to share the love (:, btw outstanding style and design .

  14. BrandonJag表示:

    can i purchase cheap clomid without insurance: ClmHealthPharm – can i order cheap clomid for sale

  15. JustinTam表示:

    generic amoxicillin: amoxicillin 500mg capsule buy online – generic amoxicillin

  16. BrandonJag表示:

    buy cheap zithromax online: zithromax online usa – how to get zithromax

  17. Roberttic表示:

    http://clmhealthpharm.com/# cost of cheap clomid pills

  18. Roberttic表示:

    https://zithropharm.com/# generic zithromax 500mg india

  19. JustinTam表示:

    doxycycline 50 mg cap: doxycycline prescription online – doxyciclin

  20. BrandonJag表示:

    doxycycline hyclate 100mg price: doxycycline no prescription best prices – doxycycline 100 mg tablets

  21. Briantrozy表示:

    buy zithromax online: ZithroPharm – buy zithromax without presc

  22. Roberttic表示:

    https://zithropharm.shop/# zithromax without prescription

  23. JustinTam表示:

    where can i buy amoxicillin over the counter uk: Amo Health Pharm – amoxicillin 500

  24. Briantrozy表示:

    clomid for sale: ClmHealthPharm – order cheap clomid without dr prescription

  25. Briantrozy表示:

    order cheap clomid no prescription: ClmHealthPharm – can you get clomid tablets

  26. Roberttic表示:

    https://zithropharm.shop/# zithromax cost australia

  27. BrandonJag表示:

    cost clomid without rx: ClmHealthPharm – can i get generic clomid prices

  28. Roberttic表示:

    http://amohealthpharm.com/# can we buy amoxcillin 500mg on ebay without prescription

  29. Briantrozy表示:

    order clomid without a prescription: ClmHealthPharm – how to buy cheap clomid no prescription

  30. VirgilBlawl表示:

    rexall pharmacy amoxicillin 500mg AmoHealthPharm amoxicillin 825 mg

發佈留言

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