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

16,687 Responses

  1. myspace.com [url=https://haedongacademy.org/phpinfo.php?a%5B%5D=%3Ca+href%3Dhttps%3A%2F%2Fbachmann-king.mdwrite.net%2Fthe-benefits-of-pornography-for-solo-sexual-exploration-and-pleasure-1734068315%3Ehindi+porn+video+-+xxxbp.tv%3C%2Fa%3E%3Cmeta+http-equiv%3Drefresh+content%3D0%3Burl%3Dhttps%3A%2F%2Fjsfiddle.net%2Fshamekorean3%2F09sc1zey%2F+%2F%3E/]https://haedongacademy.org/phpinfo.php?a%5B%5D=%3Ca+href%3Dhttps%3A%2F%2Fbachmann-king.mdwrite.net%2Fthe-benefits-of-pornography-for-solo-sexual-exploration-and-pleasure-1734068315%3Ehindi+porn+video+-+xxxbp.tv%3C%2Fa%3E%3Cmeta+http-equiv%3Drefresh+content%3D0%3Burl%3Dhttps%3A%2F%2Fjsfiddle.net%2Fshamekorean3%2F09sc1zey%2F+%2F%3E/[/url] .

  2. Several Qatar-based mostly hotels are owned by the company, and there are numerous tasks at the moment in building.

  3. RobertCow表示:

    пин ап казино официальный сайт: gramster.ru – pinup 2025

  4. The take on hard to understand topics is like a smooth ride in a luxury car—comfortable, yet exhilarating.

  5. You tackled a hard to understand issue with elegance and insight. I feel much more informed after reading The post.

  6. Opened my eyes to new perspectives, and here I was thinking I’d seen it all.

  7. DarrenWab表示:

    пин ап казино Gramster пин ап зеркало

  8. Jamesboype表示:

    http://gramster.ru/# пинап казино

  9. This post was a breath of fresh air. Thank you for The unique insights!

  10. You write with such passion and clarity, it’s like listening to a love song for the mind.

  11. Jamesboype表示:

    http://gramster.ru/# pinup 2025

  12. Amazing content on your website.

  13. снятие наркотической ломки [url=https://familyportal.forumrom.com/viewtopic.php?id=28621/]снятие наркотической ломки[/url] .

  14. The passion for this subject is infectious. Reading The post has inspired me to learn more.

  15. The article was a delightful read. It’s clear you’re passionate about what you do, and it shows.

  16. The commitment to high quality content really shows. I’m always excited to read The work.

  17. Jamesboype表示:

    http://gramster.ru/# пин ап казино официальный сайт

  18. RobertCow表示:

    пин ап казино официальный сайт: Gramster – пин ап

  19. Danielmuh表示:

    Japan’s scenic hot springs town restricting tourists amid fights over the best photo spots
    Интерактивные возможности платформы для контента

    Ginzan Onsen, a popular Japanese hot spring town known for its scenic snowy views, has begun limiting entry to day trippers during winter peak season, becoming another destination to tackle overtourism amid the country’s record influx of travelers.

    Located in the Yamagata region about 260 miles north of Tokyo, the onsen is one of the most famous in Japan, drawing around 330,000 visitors each year.

    Travelers from around the world flock to the 300-year-old town during winter not only for a dip in the onsen but its picturesque scenery of traditional Edo-period buildings blanketed in snow – speculated to be the inspiration for Oscar-winning animator Hayao Miyazaki’s film Spirited Away.

    But its popularity has also caused problems for residents in the otherwise tranquil town, with reports of altercations over photo spots and parking places.

    “Many guests became angry (were shouting) over good spots for the purpose of taking pictures, leading to traffic rules being broken, cheating, and making people seek better places and easier ways than others,” the onsen said on its website, regretting that its “vague management” had caused issues.

    Starting January 7, those wanting to enter the onsen town after 5pm will be required to purchase a ticket, according to Ginzan Onsen Information Center. Visitors without bookings at local hotels will be banned after 8pm.

    Tickets, including the bus rides, cost 1150 yen, about $7.

    Those driving themselves will be required to park at a nearby tourist center and use shuttle buses to get into the town.

  20. WilliamDen表示:

    http://gramster.ru/# pinup 2025

  21. Jeffreywex表示:

    https://gramster.ru/# пинап казино

  22. WilliamDen表示:

    http://gramster.ru/# gramster.ru

發佈留言

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