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

6,608 Responses

  1. wewencash表示:

    Combination therapy is suggested for the treatment of moderate to severe infections, even if a single agent demonstrates activity. doxycycline for mrsa

  2. Arreple表示:

    doxycycline genital infection DOX did not exert any significant change in liver except dilatations of sinusoid capillaries.

  3. Ediuvk表示:

    cheap fexofenadine 120mg allegra 180mg price generic panadol

  4. arrindCen表示:

    This was later to change. doxycycline for std

  5. wewencash表示:

    2008 Mar 25; 70 13 986- 7. doxycycline killed my dog

  6. Juanitapisn表示:

    adult singles
    free and best
    online-dating-ukraine

  7. MichaelBlibe表示:

    buy clomiphene online buy clomiphene

  8. Willietwict表示:

    https://24hr-pharmacy.top/# canadian drugs online pharmacy

  9. Brianagite表示:

    https://24hr-pharmacy.top/# buying from canadian online pharmacies

  10. Mavispisn表示:

    online dating match
    new dating
    online dating women

  11. MichaelBlibe表示:

    buy clomid for men buy clomid for men

  12. Taefwk表示:

    order acyclovir 400mg generic zovirax online perindopril 8mg pills

  13. Willietwict表示:

    https://doxycycline.pro/# how to get doxycycline without prescription

  14. JimmyEvags表示:

    ivermectin 500ml ivermectin mange treatment dosage

  15. endearype表示:

    does tamoxifen make you tired Never having done this before but consulted and almost started the process with another clinic she made us feel safe and explained every step and process and was candid.

  16. DavidResee表示:

    ivermectin for dogs dosage oral ivermectin for guinea pigs

  17. spervenef表示:

    We also measure estradiol levels on a regular basis during gonadotropin stimulation in order to optimize gonadotropin dosing. tamoxifen and weight gain

  18. ScottNub表示:

    stromectol lice what is stromectol

  19. endearype表示:

    All rights reserved. tamoxifen metabolism Usually take between cycle therapy to produce more time frame.

  20. endearype表示:

    Intention to treat analysis or loss to follow up was not mentioned. is a half dose of tamoxifen better than none

  21. WillieAttiz表示:

    https://stromectol1st.com/# ivermectin egg withdrawal

  22. DavidResee表示:

    stromectol ingredient what does ivermectin treat in dogs

  23. DavidResee表示:

    stromectol where to buy ivermectin dose for head lice

發佈留言

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