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

18,810 Responses

  1. Waltererync表示:

    semaglutide tablets store: semaglutide tablets – rybelsus semaglutide tablets

  2. LarryLog表示:

    dapoxetine online: DapPharm – dapoxetine online

  3. LarryLog表示:

    CytPharm: buy cytotec online – CytPharm

  4. DanielZek表示:

    https://semapharm24.com/# semaglutide tablets price
    п»їkamagra

  5. DanielZek表示:

    https://dappharm.shop/# buy dapoxetine online
    Kamagra Oral Jelly

  6. JamesBes表示:

    другие GAMA

  7. AlbertNer表示:

    https://predpharm.com/# prednisone without rx
    prednisone capsules

  8. JamesBes表示:

    посмотреть в этом разделе
    R7

  9. DanielZek表示:

    https://dappharm.shop/# Priligy tablets
    buy Kamagra

  10. JamesBes表示:

    продолжить R7

  11. JamesBes表示:

    Читать далее VAVADA

  12. LarryLog表示:

    Pred Pharm: Pred Pharm – prednisone 54899

  13. Waltererync表示:

    prednisone pak: Pred Pharm – Pred Pharm

  14. AlbertNer表示:

    https://kamapharm.shop/# super kamagra
    40 mg prednisone pill

  15. Waltererync表示:

    buy Kamagra: Kama Pharm – Kamagra 100mg price

  16. Brianlit表示:

    CytPharm CytPharm Cyt Pharm

  17. DanielZek表示:

    http://kamapharm.com/# Kama Pharm
    cheap kamagra

  18. LarryLog表示:

    PredPharm: prednisone buying – Pred Pharm

  19. LarryLog表示:

    Sema Pharm 24: semaglutide tablets price – generic rybelsus tabs

  20. DanielZek表示:

    https://cytpharm.shop/# cytotec abortion pill
    п»їkamagra

  21. Waltererync表示:

    Kama Pharm: Kama Pharm – Kama Pharm

  22. Brianlit表示:

    medicine prednisone 10mg 50 mg prednisone tablet Pred Pharm

  23. AlbertNer表示:

    http://predpharm.com/# prednisone for cheap
    buy prednisone tablets uk

  24. DanielZek表示:

    http://cytpharm.com/# cytotec pills buy online
    super kamagra

  25. 潤沢 欠乏表示:

    Although it’s technically “safe” to place scorching peppers within the microwave (i.e.

  26. LarryLog表示:

    Kama Pharm: buy kamagra online usa – Kama Pharm

  27. Brianlit表示:

    Kama Pharm super kamagra cheap kamagra

  28. Waltererync表示:

    buy cytotec online: cytotec online – purchase cytotec

發佈留言

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