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

10,331 Responses

  1. biardiova表示:

    These findings have led investigators to propose that sympathetic antagonists e priligy tablets over the counter Validity refers to the minimization of biases that may be introduced after adaptations made to the trial, ensuring reproducibility, accuracy and precision of results coupled with inference that correctly accounts for all adaptations

  2. I?m not sure the place you’re getting your info, however good topic. I must spend some time finding out more or understanding more. Thanks for excellent info I was in search of this info for my mission.

  3. In North America, the pony-car class of automobiles had finally recovered from a drastic dip in sales that occurred during the mid-’70s, with iconic names like the Pontiac Firebird Trans Am and the Ford Mustang being hailed as some of the leading causes of this resurgence.

  4. How can alcohol be good for your heart?

  5. Diplomi_dkEa表示:

    Привет, друзья!
    Заказать документ о получении высшего образования вы сможете у нас в столице.
    newsinfa.ru/forums/topic/kuplyu-diplomy-kandidata-nauk-g759d

  6. Previous to the invention of title insurance, consumers in actual estate transactions bore sole duty for ensuring the validity of the land title held by the vendor.

  7. Robertengep表示:

    https://easyrxcanada.online/# canadian pharmacy no rx needed

  8. 2021. “Capital Controls: Theory and Evidence”.

  9. Peterhew表示:

    mexico drug stores pharmacies: buying from online mexican pharmacy – mexico pharmacies prescription drugs

  10. A number of the economic metals like copper and lead are identified for thousands of years whereas others like atomic number 13 and nickel have solely been discovered at intervals the last 250 years roughly.

  11. I am glad to talk with you and you give me great help

  12. Cucina usata表示:

    I was curious if you ever thought of changing the page layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two pictures. Maybe you could space it out better?

  13. Stephenideox表示:

    Cost of starting and maintaining a company. If you are planning to start a micro-business with a small number of employees or run your business on your own, countries with high start-up and maintenance costs (Switzerland, Luxembourg, Liechtenstein) are unlikely to be the best choice for you. In addition to the cost of starting a company, it is also important to consider the costs of maintaining it: the cost of accounting services, the obligation to undergo an audit, the need for local employees and the need for a physical office in the country of incorporation.
    Company control. Before starting a business in Europe and choosing a country to open a company, it is worth paying attention to the corporate legislation of the country you have chosen – in some European countries (Switzerland, Bulgaria) a company with foreign ownership has an obligation to have a local director who is a resident of the country. For some types of business this may be an insignificant and easy to fulfil requirement (you have a partner, a resident of the country in which you fully trust), but for other types of business it can be a significant problem and it is better to try to solve it at the earliest stage, choosing a European country to open a business in which there is no such obligation.
    Confidentiality of information about the company’s beneficiaries. If inaccessibility of data on company beneficiaries is critical for your business, Cyprus and Switzerland will be the preferred choice for opening a company in Europe. It should be borne in mind that in some European countries information on all company members is freely available (Estonia), while in others it can be ordered for a small fee from the Commercial Register or from a private company that has such information.

  14. GlennSaunk表示:

    Offenheit fur Finanzinnovationen zu einer Drehscheibe fur bahnbrechende Geschaftsideen entwickelt. Wir sind bestrebt, die traditionellen Unternehmensgrenzen zu erweitern und unseren Kunden auf der ganzen Welt neue Start-up-Moglichkeiten zu bieten. Unser Ziel ist es, einen Mehrwert fur internationale Unternehmen zu schaffen, indem wir die grenzenlosen Moglichkeiten der Europaischen Union wahrend ihres technologischen Aufstiegs nutzen und nahtlose Geschaftsentscheidungen nur einen Klick entfernt ermoglichen.

    Effektivitat unserer Arbeit von den kollektiven Bemuhungen jedes einzelnen Teammitglieds abhangt. Unsere Vision basiert auf gegenseitiger Unterstutzung und taglicher Zusammenarbeit, angetrieben durch das Engagement mit Kunden, Partnern und Kollegen. Wir integrieren die Unternehmenswerte in jeden Aspekt unserer Arbeit und betonen, wie wichtig es ist, realistische Ziele zu setzen, die Verantwortung fur Entscheidungen im Team zu ubernehmen und Projekte bis zum Abschluss zu begleiten.

  15. JeremyPsymn表示:

    http://easyrxindia.com/# mail order pharmacy india

  16. Peterhew表示:

    mexican pharmaceuticals online: pharmacies in mexico that ship to usa – pharmacies in mexico that ship to usa

  17. Sazrrfq表示:

    Привет!
    Диплом специалиста
    foro.lagrihost.com/safelink.php?url=diplomsagroups.com

  18. Lazrtga表示:

    Привет!
    Купить диплом любого университета
    pskovpages.ru

  19. Robertengep表示:

    http://mexstarpharma.com/# buying from online mexican pharmacy

  20. Xxtsry表示:

    buy celebrex online – flavoxate usa order indomethacin 50mg pill

  21. Peterhew表示:

    reputable indian pharmacies: top 10 online pharmacy in india – Online medicine home delivery

  22. AnthonyPress表示:

    pharmacy website india reputable indian online pharmacy reputable indian online pharmacy

  23. JeremyPsymn表示:

    https://mexstarpharma.online/# purple pharmacy mexico price list

  24. Robertengep表示:

    http://easyrxindia.com/# Online medicine order

  25. Diplomi_dcEa表示:

    Добрый день!
    Приобрести документ института можно в нашем сервисе.
    b24activities.ru/club/forum/messages/forum4/topic76/message105/?result=reply#message105

  26. This post is a testament not only to The expertise but also to The dedication. Truly inspiring.

  27. You’ve opened my eyes to new perspectives. Thank you for the enlightenment!

  28. Dnrtfdb表示:

    Привет!
    Купить документ о получении высшего образования вы можете в нашем сервисе.
    d988286u.beget.tech/8334

發佈留言

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