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

  1. Roberttic表示:

    http://doxhealthpharm.com/# doxycycline 25mg

  2. BrandonJag表示:

    amoxicillin 500 mg for sale: order amoxicillin online no prescription – amoxacillian without a percription

  3. Briantrozy表示:

    how to get generic clomid no prescription: how can i get cheap clomid without insurance – where buy cheap clomid pill

  4. BrandonJag表示:

    zithromax for sale 500 mg: ZithroPharm – can you buy zithromax over the counter

  5. Briantrozy表示:

    doxycycline generic: doxycycline discount – doxycycline tablet 100 mg

  6. You might have the ability to get away with carrying a decrease SPF sunscreen than your fair mates, though.

  7. VirgilBlawl表示:

    where to buy cheap clomid prices get generic clomid without rx can i order generic clomid without rx

  8. JustinTam表示:

    doxycycline 40mg capsules: doxycycline 50 mg price uk – doxycycline canada pharmacy

  9. Roberttic表示:

    https://zithropharm.shop/# zithromax 500mg

  10. Roberttic表示:

    https://doxhealthpharm.shop/# doxycycline 150 mg tablets

  11. BrandonJag表示:

    where buy cheap clomid now: can i purchase clomid – where buy cheap clomid without insurance

  12. JustinTam表示:

    where to buy generic clomid now: ClmHealthPharm – order clomid without a prescription

  13. VirgilBlawl表示:

    purchase amoxicillin online Amo Health Pharm amoxil pharmacy

  14. It will be significant for fogeys and caregivers to be conscious of their interactions with infants and provide constant and nurturing care to encourage the development of secure attachment.

  15. BrandonJag表示:

    where can i buy zithromax capsules: buy azithromycin zithromax – zithromax z-pak price without insurance

  16. BrandonJag表示:

    where buy clomid without a prescription: ClmHealthPharm – can i purchase clomid price

  17. JustinTam表示:

    get cheap clomid for sale: ClmHealthPharm – clomid brand name

  18. VirgilBlawl表示:

    buy zithromax online with mastercard zithromax antibiotic without prescription zithromax coupon

  19. BrandonJag表示:

    zithromax tablets for sale: ZithroPharm – can you buy zithromax over the counter in australia

  20. JustinTam表示:

    generic zithromax azithromycin: Zithro Pharm – how to get zithromax online

  21. Richardfloda表示:

    Вакансии Москва – Метро Москвы, Ищу модель Москва

  22. sv388表示:

    This is the wave – the big wave.

  23. Richardfloda表示:

    Москва с вами – Ищу модель Москва, Город Москва

  24. Roberttic表示:

    http://clmhealthpharm.com/# how to get generic clomid without prescription

  25. VirgilBlawl表示:

    doxycycline rx coupon DoxHealthPharm price of doxycycline

  26. Briantrozy表示:

    doxycycline capsules purchase: Dox Health Pharm – where to get doxycycline

  27. Briantrozy表示:

    amoxicillin online without prescription: amoxicillin online purchase – generic amoxicillin cost

  28. BrandonJag表示:

    can i buy cheap clomid price: ClmHealthPharm – where buy cheap clomid

  29. Briantrozy表示:

    amoxicillin pharmacy price: AmoHealthPharm – generic amoxicillin cost

  30. Richardfloda表示:

    Подслушано Москва – Население Москвы, Барахолка Москва

發佈留言

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