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

  1. 홍대룸表示:

    Pretty impressive article. I just stumbled upon your site and wanted to say that I have really enjoyed reading your opinions. Any way I’ll be coming back and I hope you post again soon.

  2. Sure, the resemblance to your French bulldog is nil, but remember this necessary reality: like all canines, your French bulldog is a pack animal.

  3. しな 中国表示:

    You’ll be able to follow Miko Carreon on his Instagram account right here.

  4. Cherished is likely to be what people say about your comments.

  5. The applications used their very own 4-piece endgame databases.

  6. Checklist of K-pop songs on the Billboard Japan Sizzling a hundred is a compilation of weekly chart data for Okay-pop music printed on the Billboard Japan Scorching one hundred chart by the Billboard charts, and reported on by Billboard Okay-Town, an internet Billboard column.

  7. CoreyNeura表示:

    semaglutide tablets store semaglutide tablets semaglutide tablets price

  8. BradleyPobby表示:

    https://cenforce.icu/# cenforce for sale

  9. BradleyPobby表示:

    https://drugs1st.pro/# drugs1st

  10. StevenAxofs表示:

    https://edpills.men/# what is the cheapest ed medication

  11. casino表示:

    I am glad to be a visitor of this thoroughgoing web blog ! , regards for this rare information! .

  12. StevenAxofs表示:

    https://kamagra.men/# sildenafil oral jelly 100mg kamagra

  13. JamesThync表示:

    sildenafil oral jelly 100mg kamagra: cheap kamagra – Kamagra 100mg price

  14. 홍대룸表示:

    My brother suggested I might like this websiteHe was once totally rightThis post truly made my dayYou can not imagine simply how a lot time I had spent for this information! Thanks!

  15. Greetings… your blog is very interesting and beautifully written.

  16. 피파대낙表示:

    Tips and tools you offer are so helpful to agencies in our community.

  17. JamesThync表示:

    cenforce: cenforce – Purchase Cenforce Online

  18. Charlescek表示:

    semaglutide tablets price: semaglutide tablets – buy semaglutide

  19. StevenAxofs表示:

    http://drugs1st.pro/# drugs1st

  20. You may observe Angela Martinez on her Instagram account here.

  21. Charlescek表示:

    Purchase Cenforce Online: cheapest cenforce – cenforce for sale

  22. JamesThync表示:

    drugs1st: erection pills online – drugs1st

  23. BradleyPobby表示:

    https://drugs1st.pro/# drugs1st

  24. StevenAxofs表示:

    http://drugs1st.pro/# drugs1st

  25. BradleyPobby表示:

    https://drugs1st.pro/# drugs1st

發佈留言

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