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

1,522 Responses

  1. Lfbljq表示:

    order colchicine generic colchicine 0.5mg brand buy clopidogrel online cheap

  2. Chelsaepisn表示:

    college essay help long island
    help with writing an essay
    best writing paper

  3. Calvindit表示:

    ed medication online ed medications online

  4. Nanicepisn表示:

    best custom essay service
    help my essay
    reviews of essay writing services

  5. Calvindit表示:

    ed dysfunction treatment new treatments for ed

  6. Thhlpo表示:

    buy prograf 5mg pill tricor generic buy generic tricor 160mg

  7. Charitapisn表示:

    common application essay help
    write my admissions essay
    custom essay writing reviews

  8. Edpwal表示:

    avapro pills pamelor 25mg pill buy pepcid 40mg pill

  9. Calvindit表示:

    canadian pharmacy discount coupon cheapest prescription pharmacy

  10. Charitapisn表示:

    what is a good essay writing service
    help me write a essay
    help writing scholarship essays

  11. Calvindit表示:

    cheapest pharmacy prescription drugs canadian pharmacy coupon

  12. Ashlenpisn表示:

    academic essay writers
    cheap essays to buy
    custom essay meister

  13. Oorliq表示:

    clarinex drug generic glimepiride buy mesalamine

  14. Calvindit表示:

    cheapest pharmacy for prescriptions rx pharmacy coupons

  15. Merolapisn表示:

    help me write a narrative essay
    cheap custom essays online
    best essay writers online

  16. Calvindit表示:

    cheap erectile dysfunction pill erection pills that work

發佈留言

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