踩到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,106 Responses

  1. BrandonJag表示:

    amoxicillin price canada: AmoHealthPharm – amoxicillin 500mg no prescription

  2. JustinTam表示:

    doxycycline over the counter singapore: DoxHealthPharm – doxycycline over the counter canada

  3. Roberttic表示:

    https://amohealthpharm.com/# generic amoxicillin cost

  4. JustinTam表示:

    generic clomid online: ClmHealthPharm – clomid medication

  5. VirgilBlawl表示:

    can i order generic clomid now get cheap clomid for sale cost of clomid no prescription

  6. VirgilBlawl表示:

    how to order doxycycline DoxHealthPharm doxycycline 100 mg cap over the counter

  7. JustinTam表示:

    where to buy amoxicillin 500mg without prescription: amoxicillin order online – amoxicillin 500mg over the counter

  8. 372393 331891very nice put up, i definitely enjoy this web website, maintain on it 62923

  9. BrandonJag表示:

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

  10. Roberttic表示:

    http://zithropharm.com/# zithromax 250 price

  11. JustinTam表示:

    where buy cheap clomid now: cheap clomid without a prescription – can i get cheap clomid without prescription

  12. Roberttic表示:

    https://doxhealthpharm.shop/# where to buy doxycycline in australia

  13. BrandonJag表示:

    where buy cheap clomid pill: can you buy cheap clomid without dr prescription – how to get clomid pills

  14. Briantrozy表示:

    amoxicillin online canada: AmoHealthPharm – amoxicillin online purchase

  15. Briantrozy表示:

    clomid order: ClmHealthPharm – can i buy generic clomid without rx

  16. smm-panel表示:

    boost of subscribers in telegram channel youtube boost

  17. BrandonJag表示:

    buy generic clomid: clomid pills – can i order clomid for sale

  18. Roberttic表示:

    https://clmhealthpharm.shop/# can you buy generic clomid no prescription

  19. JustinTam表示:

    can you buy zithromax over the counter: Zithro Pharm – zithromax

  20. Glad to be one of several visitors on this awful internet site : D.

  21. Briantrozy表示:

    80 mg doxycycline: DoxHealthPharm – how can i get doxycycline

  22. Roberttic表示:

    https://clmhealthpharm.shop/# where buy cheap clomid no prescription

  23. BrandonJag表示:

    how to buy amoxycillin: order amoxicillin uk – amoxicillin 500mg over the counter

  24. Roberttic表示:

    https://zithropharm.shop/# buy zithromax canada

  25. JustinTam表示:

    zithromax for sale usa: zithromax 250 mg pill – buy zithromax online with mastercard

  26. JustinTam表示:

    over the counter amoxicillin canada: amoxicillin in india – amoxicillin canada price

  27. smm-panel表示:

    boost of subscribers in telegram live telegram boost

  28. BrandonJag表示:

    buy zithromax canada: Zithro Pharm – zithromax 500mg price

  29. BrandonJag表示:

    how to get generic clomid without dr prescription: ClmHealthPharm – cost of clomid online

發佈留言

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