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

30,351 Responses

  1. Tracyseils表示:

    migliori farmacie online 2023: cialis generico – farmacie online sicure

  2. Tracyseils表示:

    farmacia online migliore: farmacia online miglior prezzo – farmacie online affidabili

  3. TerryRam表示:

    farmacie online sicure farmacia online acquisto farmaci con ricetta

  4. Sidneycrurn表示:

    https://sildenafilit.bid/# pillole per erezione in farmacia senza ricetta

  5. Tracyseils表示:

    farmacia online migliore: farmacie online affidabili – top farmacia online

  6. Sonnyaftem表示:

    farmacia online piГ№ conveniente: kamagra – farmacie online affidabili

  7. Tracyseils表示:

    viagra originale in 24 ore contrassegno: viagra consegna in 24 ore pagamento alla consegna – pillole per erezione in farmacia senza ricetta

  8. Michaelnot表示:

    https://gabapentin.life/# order neurontin

  9. MichaelOwend表示:

    buy paxlovid online: buy paxlovid – paxlovid price

  10. Michaelnot表示:

    https://wellbutrin.rest/# wellbutrin 75 mg tablet

  11. Michaelnot表示:

    https://clomid.club/# where can i buy cheap clomid online

  12. MichaelOwend表示:

    32 neurontin: gabapentin best price – neurontin singapore

  13. Michaelnot表示:

    http://clomid.club/# can i buy clomid online

  14. MichaelOwend表示:

    get clomid online: clomid best price – where can i buy cheap clomid without a prescription

  15. Mathewtow表示:

    Paxlovid buy online https://paxlovid.club/# paxlovid

  16. Michaelnot表示:

    https://clomid.club/# get generic clomid tablets

  17. MichaelOwend表示:

    how to get wellbutrin in australia: buy wellbutrin – wellbutrin 15 mg

  18. Michaelnot表示:

    http://claritin.icu/# buy ventolin online cheap

  19. MichaelOwend表示:

    buy paxlovid online: Paxlovid price without insurance – Paxlovid buy online

  20. Michaelnot表示:

    https://clomid.club/# how can i get cheap clomid no prescription

  21. MichaelOwend表示:

    over the counter neurontin: cheap gabapentin – medication neurontin

  22. sitemap表示:

    Hi colleagues, hoow is everything, aand whbat youu
    desiree tto sayy concerninmg this post, inn my view iits acually awesome iin favor off me.

  23. JamesSpisk表示:

    https://mexicopharmacy.store/# mexican online pharmacies prescription drugs

  24. RobertRix表示:

    reputable indian online pharmacy: best india pharmacy – reputable indian online pharmacy

  25. Richardves表示:

    canada online pharmacy: trust canadian pharmacy – cheapest pharmacy canada

  26. JamesSpisk表示:

    http://ordermedicationonline.pro/# canadian neighbor pharmacy legit

  27. DanielDit表示:

    onlinecanadianpharmacy safe online pharmacy canadian pharmacy antibiotics

  28. RobertRix表示:

    no prescription rx medicine: buy drugs online – my canadian pharmacy rx reviews

  29. sitemap表示:

    Heya i aam ffor tthe fiirst tjme here. I cwme acros this board andd
    I fiind It reazlly useful & it helpedd me outt much. I ope to give something backk and aid otjers like you aided me.

  30. Richardves表示:

    buy medicines online in india: top online pharmacy india – india online pharmacy

發佈回覆給「Richardves」的留言 取消回覆

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