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

17,013 Responses

  1. Diplomi_axpi表示:

    купить диплом медицинского вуза купить диплом медицинского вуза .

  2. LarryHom表示:

    https://mexicanpharmacy.store/# п»їbest mexican online pharmacies

  3. Brianchoky表示:

    http://mexicanpharmacy.store/# mexican rx online

  4. Jamestus表示:

    buy prescription drugs from india top 10 pharmacies in india best online pharmacy india

  5. Brianchoky表示:

    http://indianpharmacy.win/# india online pharmacy

  6. ClaytonGauct表示:

    Online medicine order: online shopping pharmacy india – buy prescription drugs from india

  7. sport-weekend.com [url=omskregion.info/news/138721-elektronne_proxodne_obespechenie_bezopasnosti_i_ko/]omskregion.info/news/138721-elektronne_proxodne_obespechenie_bezopasnosti_i_ko/[/url] .

  8. k8cc表示:

    I had fun reading this post. I want to see more on this subject.. Gives Thanks for writing this nice article.. Anyway, I’m going to subscribe to your rss and I wish you write great articles again soon.

  9. This retro car was launched after Ford displayed two unique 1949 Fords, one inventory and one extremely customized.

  10. sun win表示:

    I know this is not exactly on topic, but i have a blog using the blogengine platform as well and i’m having issues with my comments displaying. is there a setting i am forgetting? maybe you could help me out? thank you.

  11. Jamesknowl表示:

    https://mexicanpharmacy.store/# mexico drug stores pharmacies

  12. Brianchoky表示:

    http://mexicanpharmacy.store/# pharmacies in mexico that ship to usa

  13. bán vũ khí表示:

    The post is absolutely fantastic! Lots of great info and inspiration, both of which we all need! Also like to admire the time and effort you put into your website and detailed info you offer! I will bookmark your website!

  14. LarryHom表示:

    http://indianpharmacy.win/# Online medicine order

  15. ClaytonGauct表示:

    buying prescription drugs in mexico online: mexican border pharmacies shipping to usa – best online pharmacies in mexico

  16. вывод. из. запоя. ростов. [url=https://vyvod-iz-zapoya-rostov235.ru/]https://vyvod-iz-zapoya-rostov235.ru/[/url] .

  17. вывод из запоя стационарно ростов вывод из запоя стационарно ростов .

  18. Jamesknowl表示:

    https://mexicanpharmacy.store/# mexican pharmaceuticals online

  19. Jamesknowl表示:

    https://mexicanpharmacy.store/# mexican border pharmacies shipping to usa

  20. Jamestus表示:

    medicine in mexico pharmacies reputable mexican pharmacies online mexico pharmacies prescription drugs

  21. Brianchoky表示:

    https://mexicanpharmacy.store/# purple pharmacy mexico price list

  22. Brianchoky表示:

    https://mexicanpharmacy.store/# buying prescription drugs in mexico online

  23. ClaytonGauct表示:

    buying prescription drugs in mexico: buying prescription drugs in mexico – mexican mail order pharmacies

  24. ClaytonGauct表示:

    canadian pharmacy oxycodone: best online canadian pharmacy – canadian drugs online

  25. LarryHom表示:

    https://indianpharmacy.win/# top 10 pharmacies in india

發佈留言

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