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

  1. JamesSpisk表示:

    https://mexicopharmacy.store/# mexican drugstore online

  2. RobertRix表示:

    mexican border pharmacies shipping to usa: mexican online pharmacy – mexican rx online

  3. Richardves表示:

    cheapest online pharmacy india: reputable indian online pharmacy – top 10 pharmacies in india

  4. JamesFliva表示:

    http://doxycycline.forum/# doxycycline pharmacy

  5. Charlesrhype表示:

    amoxicillin for sale where can i buy amoxicillin over the counter uk amoxicillin 750 mg price

  6. ScottJeove表示:

    zithromax 500mg price: zithromax antibiotic without prescription – where can i get zithromax over the counter

  7. VernonTratt表示:

    doxycycline prescription cost uk: Buy Doxycycline for acne – can i buy doxycycline over the counter

  8. JamesFliva表示:

    https://ciprofloxacin.men/# buy cipro no rx

  9. Charlesrhype表示:

    lisinopril 250mg buy lisinopril prinivil medication

  10. ScottJeove表示:

    antibiotics cipro: ciprofloxacin without insurance – cipro for sale

  11. JamesFliva表示:

    https://doxycycline.forum/# where to get doxycycline in singapore

  12. ScottJeove表示:

    lisinopril 40 mg tablet price: zestril brand name – buy lisinopril online usa

  13. Charlesrhype表示:

    buy ciprofloxacin tablets Ciprofloxacin online prescription cipro for sale

  14. VernonTratt表示:

    zithromax 500: zithromax z-pak – where to get zithromax

  15. JamesFliva表示:

    https://ciprofloxacin.men/# buy cipro online canada

  16. sitemap.xml表示:

    Youu actuzlly ake iit apear realoly eazy wth your presentgation however I in fincing this topic tto be actualy something which
    I feel I mibht never understand. It seems too cmplex annd extremely wide for me.
    I amm takiing a look fprward for yojr subsequent publish, I
    will try to get thee dangle of it!

  17. ScottJeove表示:

    buy cheap generic zithromax: zithromax z-pak – purchase zithromax z-pak

  18. Charlesrhype表示:

    ciprofloxacin generic Buy ciprofloxacin 500 mg online buy cipro cheap

  19. JamesFliva表示:

    https://doxycycline.forum/# no prescription doxycycline

  20. sitemap.xml表示:

    It’s neazrly impossible too fund educated people ahout
    this subject, however, you sound like youu know what you’re talkng
    about!Thanks

  21. sitemap.xml表示:

    I’ve beesn browsijg on-line greater than thtee hours lately,
    but I never discovered anyy attention-grabbing artile like yours.
    It’s pretyy price enough for me. In my opinion,if all website owners andd
    bloggers made justt rjght content matedrial ass you did, tthe internbet miight bbe
    much moore hhelpful than eer before.

  22. VernonTratt表示:

    can you buy amoxicillin over the counter in canada: amoxicillin 250 mg – amoxicillin 500mg capsules

  23. ScottJeove表示:

    cost of doxycycline tablets: doxycycline 100mg price in south africa – doxycycline tablets canada

  24. Charlesrhype表示:

    can you buy zithromax over the counter in mexico zithromax z-pak zithromax for sale 500 mg

  25. JamesFliva表示:

    http://azithromycin.bar/# buy zithromax 1000 mg online

  26. sitemap表示:

    Greetings! Verry helpful advicee within this post!
    It’s the little changes thwt makle the most significant
    changes. Thanks for sharing!

  27. sitemap.xml表示:

    Hello, just wanted too tell you, I loved this blolg post.
    It was practical. Keeep onn posting!

  28. sitemap表示:

    Thanks ffor thee auspicious writeup. It in fact was a lleisure achount it.
    Gllance complicated tto mofe delivered agreeable from
    you! However, hhow could wee communicate?

  29. ScottJeove表示:

    buy cheap amoxicillin: purchase amoxicillin online – buy amoxicillin online cheap

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

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