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

27,801 Responses

  1. Michaelgax表示:

    buy gabapentin online gabapentin for sale neurontin 204

  2. NicolasLoole表示:

    https://gabapentin.auction/# neurontin 100mg capsule price

  3. Stevenjaw表示:

    https://semaglutide.win/# semaglutide
    zithromax for sale usa

  4. SamuelCic表示:

    https://zithromax.company/# buy cheap zithromax online

  5. Michaelgax表示:

    stromectol 15 mg stromectol best price ivermectin 1 cream

  6. NicolasLoole表示:

    http://stromectol.agency/# ivermectin canada

  7. SamuelCic表示:

    http://stromectol.agency/# stromectol medication

  8. Matthewweind表示:

    generic stromectol: stromectol for sale – ivermectin 80 mg

  9. Stevenjaw表示:

    http://gabapentin.auction/# neurontin prescription coupon
    zithromax coupon

  10. Matthewweind表示:

    ivermectin pills human: stromectol best price – minocycline 100mg otc

  11. NicolasLoole表示:

    https://semaglutide.win/# Buy semaglutide pills

  12. Michaelgax表示:

    zithromax 500mg zithromax best price zithromax cost canada

  13. go88表示:

    Thanks For This Blog, was added to my bookmarks.

  14. Rogeliobib表示:

    Найти специалиста по независимой экспертизе и оценке!

    Сайт-агрегатор компаний и услуг в сфере независимой экспертизы и оценки.
    Мы создали этот проект, чтобы помочь вам найти надежных и опытных профессионалов
    для решения ваших задач.

    Главная цель — сделать процесс поиска специалистов по независимой экспертизе
    и оценке максимально простым и эффективным. Мы стремимся предоставить вам
    доступ к компаниям, которые гарантируют высокое качество услуг.
    С нами вы сможете быстро найти нужного эксперта и сравнить различные предложения.

    На нашем сайте собраны карточки компаний, каждая из которых содержит подробную
    информацию о предоставляемых услугах. Посетители могут фильтровать предложения
    по различным критериям:

    Локация
    Направление экспертизы
    Стоимость услуг
    Отзывы клиентов

    Наш сайт Специалисты по независимой экспертизе и оценке.

  15. Michaelgax表示:

    ivermectin buy australia stromectol best price minocycline 50 mg acne

  16. Stevenjaw表示:

    http://zithromax.company/# buy zithromax canada
    generic zithromax online paypal

  17. If yoᥙ аre ցoing fⲟr bet ⅽontents lie myѕelf, ϳust visit thiѕ site every day for
    the reason tһat it ցives quality cߋntents, tһanks

    Here is myy web site – تور قونیه نوروز – 24lancer.ir

  18. SamuelCic表示:

    https://zithromax.company/# zithromax 1000 mg online

  19. One thing I’d prefer to discuss is that weight loss program fast can be carried out by the correct diet and exercise. A person’s size not only affects appearance, but also the quality of life. Self-esteem, depressive disorders, health risks, and physical skills are damaged in an increase in weight. It is possible to do everything right and at the same time having a gain. In such a circumstance, a medical problem may be the perpetrator. While too much food rather than enough body exercise are usually responsible, common medical conditions and traditionally used prescriptions could greatly add to size. Thanks a bunch for your post right here.

  20. Matthewweind表示:

    generic zithromax 500mg: order zithromax – zithromax capsules price

  21. NicolasLoole表示:

    https://gabapentin.auction/# prescription drug neurontin

  22. Cmqugs表示:

    generic augmentin – clavulanate price cheap levothroid generic

  23. Stevenjaw表示:

    https://amoxil.llc/# amoxicillin no prescription
    zithromax 500mg

  24. SamuelCic表示:

    https://amoxil.llc/# amoxil pharmacy

  25. Matthewweind表示:

    stromectol price: cheapest stromectol – stromectol 3 mg

  26. Michaelgax表示:

    zithromax 250 mg pill can i buy zithromax online zithromax over the counter

  27. Michaelgax表示:

    Rybelsus 14 mg Buy compounded semaglutide online rybelsus price

  28. Stevenjaw表示:

    http://semaglutide.win/# buy rybelsus
    zithromax 250 mg tablet price

  29. нарколог на дом вывод из запоя краснодар https://vyvod-iz-zapoya-krasnodar17.ru .

發佈回覆給「تور قونیه نوروز – 24lancer.ir」的留言 取消回覆

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