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

18,705 Responses

  1. MichaelUsaws表示:

    https://farmabrufen.shop/# Farma Brufen
    Farmacia online piГ№ conveniente

  2. Edwardopele表示:

    farmaci senza ricetta elenco: Cialis senza ricetta – farmaci senza ricetta elenco

  3. Edwardopele表示:

    Farmacia online miglior prezzo: Ibuprofene 600 prezzo senza ricetta – п»їFarmacia online migliore

  4. Franktaurl表示:

    A brief history of sunglasses, from Ancient Rome to Hollywood
    kraken официальный сайт

    Sunglasses, or dark glasses, have always guarded against strong sunlight, but is there more to “shades” than we think?

    The pupils of our eyes are delicate and react immediately to strong lights. Protecting them against light — even the brilliance reflected off snow — is important for everyone. Himalayan mountaineers wear goggles for this exact purpose.

    Protection is partly the function of sunglasses. But dark or colored lens glasses have become fashion accessories and personal signature items. Think of the vast and famous collector of sunglasses Elton John, with his pink lensed heart-shaped extravaganzas and many others.

    When did this interest in protecting the eyes begin, and at what point did dark glasses become a social statement as well as physical protection?
    The Roman Emperor Nero is reported as holding polished gemstones to his eyes for sun protection as he watched fighting gladiators.

    We know Canadian far north Copper Inuit and Alaskan Yupik wore snow goggles of many kinds made of antlers or whalebone and with tiny horizontal slits. Wearers looked through these and they were protected against the snow’s brilliant light when hunting. At the same time the very narrow eye holes helped them to focus on their prey.

    In 12th-century China, judges wore sunglasses with smoked quartz lenses to hide their facial expressions — perhaps to retain their dignity or not convey emotions.

  5. It guides the company to build a balanced portfolio which will optimize their returns and also lessen the risk the companies have to take while taking the investment decisions.

  6. NathanSal表示:

    viagra prezzo farmacia 2023 viagra farmacia viagra originale in 24 ore contrassegno

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

  8. Allanlix表示:

    A brief history of sunglasses, from Ancient Rome to Hollywood
    kraken официальный сайт

    Sunglasses, or dark glasses, have always guarded against strong sunlight, but is there more to “shades” than we think?

    The pupils of our eyes are delicate and react immediately to strong lights. Protecting them against light — even the brilliance reflected off snow — is important for everyone. Himalayan mountaineers wear goggles for this exact purpose.

    Protection is partly the function of sunglasses. But dark or colored lens glasses have become fashion accessories and personal signature items. Think of the vast and famous collector of sunglasses Elton John, with his pink lensed heart-shaped extravaganzas and many others.

    When did this interest in protecting the eyes begin, and at what point did dark glasses become a social statement as well as physical protection?
    The Roman Emperor Nero is reported as holding polished gemstones to his eyes for sun protection as he watched fighting gladiators.

    We know Canadian far north Copper Inuit and Alaskan Yupik wore snow goggles of many kinds made of antlers or whalebone and with tiny horizontal slits. Wearers looked through these and they were protected against the snow’s brilliant light when hunting. At the same time the very narrow eye holes helped them to focus on their prey.

    In 12th-century China, judges wore sunglasses with smoked quartz lenses to hide their facial expressions — perhaps to retain their dignity or not convey emotions.

  9. Edwardopele表示:

    viagra online in 2 giorni: Farma Sild Italy – alternativa al viagra senza ricetta in farmacia

  10. MichaelUsaws表示:

    https://farmabrufen.shop/# BRUFEN prezzo
    Farmacia online piГ№ conveniente

  11. NathanSal表示:

    viagra online consegna rapida viagra senza ricetta viagra online spedizione gratuita

  12. Clinterete表示:

    https://farmasilditaly.shop/# viagra online in 2 giorni
    farmacia online

  13. DouglasAssow表示:

    A brief history of sunglasses, from Ancient Rome to Hollywood
    kraken тор

    Sunglasses, or dark glasses, have always guarded against strong sunlight, but is there more to “shades” than we think?

    The pupils of our eyes are delicate and react immediately to strong lights. Protecting them against light — even the brilliance reflected off snow — is important for everyone. Himalayan mountaineers wear goggles for this exact purpose.

    Protection is partly the function of sunglasses. But dark or colored lens glasses have become fashion accessories and personal signature items. Think of the vast and famous collector of sunglasses Elton John, with his pink lensed heart-shaped extravaganzas and many others.

    When did this interest in protecting the eyes begin, and at what point did dark glasses become a social statement as well as physical protection?
    The Roman Emperor Nero is reported as holding polished gemstones to his eyes for sun protection as he watched fighting gladiators.

    We know Canadian far north Copper Inuit and Alaskan Yupik wore snow goggles of many kinds made of antlers or whalebone and with tiny horizontal slits. Wearers looked through these and they were protected against the snow’s brilliant light when hunting. At the same time the very narrow eye holes helped them to focus on their prey.

    In 12th-century China, judges wore sunglasses with smoked quartz lenses to hide their facial expressions — perhaps to retain their dignity or not convey emotions.

  14. Clinterete表示:

    http://farmabrufen.com/# Farma Brufen
    farmaci senza ricetta elenco

  15. BradleyThine表示:

    migliori farmacie online 2024: Farmacie online sicure – top farmacia online
    farmacie online affidabili

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

  17. BradleyThine表示:

    farmacie online sicure: Brufen senza ricetta – comprare farmaci online all’estero
    top farmacia online

  18. NathanSal表示:

    migliori farmacie online 2024 BRUFEN prezzo migliori farmacie online 2024

  19. Clinterete表示:

    https://farmatadalitaly.shop/# top farmacia online
    comprare farmaci online con ricetta

  20. DouglasAssow表示:

    A brief history of sunglasses, from Ancient Rome to Hollywood
    kraken даркнет

    Sunglasses, or dark glasses, have always guarded against strong sunlight, but is there more to “shades” than we think?

    The pupils of our eyes are delicate and react immediately to strong lights. Protecting them against light — even the brilliance reflected off snow — is important for everyone. Himalayan mountaineers wear goggles for this exact purpose.

    Protection is partly the function of sunglasses. But dark or colored lens glasses have become fashion accessories and personal signature items. Think of the vast and famous collector of sunglasses Elton John, with his pink lensed heart-shaped extravaganzas and many others.

    When did this interest in protecting the eyes begin, and at what point did dark glasses become a social statement as well as physical protection?
    The Roman Emperor Nero is reported as holding polished gemstones to his eyes for sun protection as he watched fighting gladiators.

    We know Canadian far north Copper Inuit and Alaskan Yupik wore snow goggles of many kinds made of antlers or whalebone and with tiny horizontal slits. Wearers looked through these and they were protected against the snow’s brilliant light when hunting. At the same time the very narrow eye holes helped them to focus on their prey.

    In 12th-century China, judges wore sunglasses with smoked quartz lenses to hide their facial expressions — perhaps to retain their dignity or not convey emotions.

  21. MichaelUsaws表示:

    https://farmaprodotti.shop/# top farmacia online
    acquisto farmaci con ricetta

  22. Edwardopele表示:

    viagra acquisto in contrassegno in italia: viagra online consegna rapida – viagra online in 2 giorni

  23. Edwardopele表示:

    viagra 100 mg prezzo in farmacia: Viagra – viagra generico recensioni

  24. MichaelUsaws表示:

    https://farmaprodotti.com/# comprare farmaci online con ricetta
    farmaci senza ricetta elenco

  25. BradleyThine表示:

    pillole per erezione in farmacia senza ricetta: Viagra – viagra originale recensioni
    farmacie online affidabili

  26. промокод продамус http://www.promokod-pro.ru .

  27. 投資 どこ表示:

    Monday at St. Paul United Methodist Church with the Rev.

  28. The precious metals contracts are some of the most noted contracts traded on the Commodity Exchange (COMEX) of New York.

  29. MichaelUsaws表示:

    https://farmasilditaly.com/# pillole per erezione immediata
    comprare farmaci online con ricetta

發佈留言

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