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

  1. Martinaback表示:

    https://buytadalafil.icu/# cheapest tadalafil india

  2. ScottOxind表示:

    https://stromectolivermectin.pro/# minocycline 50 mg tablets for human

  3. AndrewBix表示:

    https://pillswithoutprescription.xyz/# canadian medications

  4. Lidrbf表示:

    buy nexium 40mg generic buy esomeprazole 40mg generic buy sumatriptan for sale

  5. ScottOxind表示:

    https://stromectolivermectin.pro/# ivermectin over the counter uk

  6. It’s too bad to check your article late. I wonder what it would be if we met a little faster. I want to exchange a little more, but please visit my site baccaratcommunity and leave a message!!

  7. FrankTrasp表示:

    https://buysildenafil.best/# sildenafil buy online usa

  8. Cthrinepisn表示:

    help with writing essays for college applications
    buy an essay online
    buy essay online cheap

  9. FrankTrasp表示:

    https://buysildenafil.best/# best price for generic sildenafil

  10. AndrewBix表示:

    https://pillswithoutprescription.xyz/# prescription drugs without doctor approval

  11. Martinaback表示:

    https://buytadalafil.icu/# buy generic tadalafil online uk

  12. CharlesCic表示:

    buy minocycline 100 mg tablets minocycline 100mg tablets online

  13. Leilahpisn表示:

    help writing a comparison and contrast essay
    essays services
    custom essay cheap

  14. CharlesCic表示:

    minocycline pills stromectol medicine

  15. Wgpfya表示:

    buy methotrexate generic generic reglan 10mg losartan drug

  16. totosite表示:

    I was looking for another article by chance and found your article totosite I am writing on this topic, so I think it will help a lot. I leave my blog address below. Please visit once.

  17. Twylapisn表示:

    custom essays services
    the help book essay
    buy essay online cheap

  18. Iowphv表示:

    order bupropion 150mg without prescription sildenafil over counter viagra sildenafil 25mg

  19. Dronapisn表示:

    buy an essay cheap
    community service essay
    order custom essays online

  20. CharlesCic表示:

    sildenafil 100mg price in india sildenafil gel

  21. CharlesCic表示:

    how much is generic sildenafil generic viagra sildenafil citrate

  22. Charopisn表示:

    please write my essay
    college admission essay editing services
    essay writer generator

  23. Ipzxjx表示:

    ibuprofen price paxil buy online cheap rocaltrol

  24. Rhiamonpisn表示:

    easy essay writer
    hire essay writer
    need help writing an essay

  25. CharlesCic表示:

    tadalafil online cost tadalafil mexico

發佈留言

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