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

  1. LarryLog表示:

    semaglutide tablets price: generic rybelsus tabs – buy semaglutide

  2. LarryLog表示:

    generic rybelsus tabs: cheap semaglutide pills – generic rybelsus tabs

  3. Brianlit表示:

    Pred Pharm 1 mg prednisone daily order prednisone 100g online without prescription

  4. DanielZek表示:

    https://predpharm.shop/# PredPharm
    Kamagra Oral Jelly

  5. AlbertNer表示:

    http://semapharm24.com/# semaglutide tablets for weight loss
    prednisone 12 tablets price

  6. Waltererync表示:

    where can i buy prednisone online without a prescription: prednisone 500 mg tablet – PredPharm

  7. пропуск для грузового транспорта в москву пропуск для грузового транспорта в москву .

  8. Waltererync表示:

    Kamagra 100mg: cheap kamagra – Kama Pharm

  9. LarryLog表示:

    Kamagra 100mg price: Kamagra Oral Jelly – п»їkamagra

  10. DanielZek表示:

    https://kamapharm.com/# Kama Pharm
    Kamagra Oral Jelly

  11. AlbertNer表示:

    https://semapharm24.shop/# Sema Pharm 24
    buy prednisone 10mg

  12. Brianlit表示:

    DapPharm buy priligy buy dapoxetine online

  13. LarryLog表示:

    CytPharm: cytotec buy online usa – cytotec pills buy online

  14. LarryLog表示:

    sildenafil oral jelly 100mg kamagra: Kama Pharm – sildenafil oral jelly 100mg kamagra

  15. AlbertNer表示:

    https://cytpharm.shop/# order cytotec online
    prednisone 20 mg

  16. Waltererync表示:

    priligy: Priligy tablets – Priligy tablets

  17. Waltererync表示:

    Pred Pharm: prednisone in mexico – can you buy prednisone over the counter in mexico

  18. Waltererync表示:

    PredPharm: 80 mg prednisone daily – buying prednisone on line

  19. AlbertNer表示:

    https://cytpharm.com/# Cyt Pharm
    where to get prednisone

  20. LarryLog表示:

    buy dapoxetine online: dap pharm – buy dapoxetine online

發佈留言

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