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

21,119 Responses

  1. Williecrady表示:

    Kasino di Indonesia menyediakan hiburan yang beragam http://preman69.tech/# Pemain bisa menikmati slot dari kenyamanan rumah

  2. DavidCooth表示:

    http://bonaslot.site/# Kasino menyediakan layanan pelanggan yang baik

  3. ThomasThype表示:

    Slot menjadi daya tarik utama di kasino: preman69 slot – preman69 slot

  4. Aaronbus表示:

    garuda888.top garuda888 slot Banyak pemain mencari mesin dengan RTP tinggi

  5. DavidCooth表示:

    http://slot88.company/# Permainan slot mudah dipahami dan menyenangkan

  6. Williecrady表示:

    Kasino sering memberikan hadiah untuk pemain setia https://bonaslot.site/# Slot klasik tetap menjadi favorit banyak orang

  7. ThomasThype表示:

    Mesin slot sering diperbarui dengan game baru: preman69 slot – preman69

  8. JamesLok表示:

    Pemain bisa menikmati slot dari kenyamanan rumah http://slotdemo.auction/# Bermain slot bisa menjadi pengalaman sosial

  9. Josephzoorp表示:

    Thieves blow up museum door and steal ancient artifacts, including 2,500-year-old gold helmet
    анальный секс можно

    Thieves have stolen four ancient artifacts, including an approximately 2,500-year-old gold helmet, after using explosives to break into a museum in the Netherlands.

    The daring heist took place at Drents Museum in Assen during the early hours of Saturday morning, according to Dutch police, who said they received a report of an explosion at 3:45 a.m. local time.

    CCTV footage released by police shows the suspects opening an exterior door before a blast sends sparks and smoke into the air. The thieves made off with three gold bracelets, dating from around 50 BC, as well as the 5th-century BC Helmet of Cotofenesti, a historically important artifact on loan from the National History Museum of Romania in Bucharest.
    The items were part of an exhibition about the Dacians, an ancient society that occupied much of present-day Romania before being conquered by the Romans. On show since July, “Dacia: Empire of Gold and Silver” featured treasures borrowed from institutions across Romania.

    In a press release on its website, Drents Museum described the Helmet of Cotofenesti — which was discovered in a Romanian village almost a century ago — as a “masterpiece.” Its design features mythological scenes and a pair of eyes, located above the wearers’, that were thought to deter enemies during battle while protecting against the “evil eye.”
    The exhibition was set to conclude Sunday, though Drents Museum remained closed through the weekend due to the robbery. Its premises were damaged by the explosion, though no injuries were reported, the museum said.

    Dutch police announced that they are working with global police agency Interpol and had, as of Sunday, received more than 50 tip-offs. Investigators are currently looking for information about a gray car that was stolen from the nearby city of Alkmaar earlier in the week and was discovered around four miles from the crime scene, on fire, shortly after the overnight heist.

  10. JamesLok表示:

    Slot menawarkan berbagai jenis permainan bonus https://garuda888.top/# Slot klasik tetap menjadi favorit banyak orang

  11. JamesLok表示:

    Permainan slot bisa dimainkan dengan berbagai taruhan http://garuda888.top/# Kasino menawarkan pengalaman bermain yang seru

  12. Aaronbus表示:

    preman69.tech preman69 Kasino selalu memperbarui mesin slotnya

  13. Williecrady表示:

    Slot dengan bonus putaran gratis sangat populer https://bonaslot.site/# Kasino menawarkan pengalaman bermain yang seru

  14. ThomasThype表示:

    Banyak pemain berusaha untuk mendapatkan jackpot: bonaslot – BonaSlot

  15. ThomasThype表示:

    Mesin slot dapat dimainkan dalam berbagai bahasa: garuda888 slot – garuda888.top

  16. DavidCooth表示:

    http://garuda888.top/# Beberapa kasino memiliki area khusus untuk slot

  17. DavidCooth表示:

    https://garuda888.top/# Kasino memastikan keamanan para pemain dengan baik

  18. Aaronbus表示:

    slot88.company slot88.company Slot klasik tetap menjadi favorit banyak orang

  19. Williecrady表示:

    Permainan slot mudah dipahami dan menyenangkan https://bonaslot.site/# Mesin slot sering diperbarui dengan game baru

  20. Williecrady表示:

    Slot dengan pembayaran tinggi selalu diminati https://preman69.tech/# Mesin slot dapat dimainkan dalam berbagai bahasa

  21. DavidCooth表示:

    http://slotdemo.auction/# Pemain sering berbagi tips untuk menang

  22. DavidCooth表示:

    http://preman69.tech/# Kasino memiliki suasana yang energik dan menyenangkan

  23. Williecrady表示:

    Slot dengan pembayaran tinggi selalu diminati http://preman69.tech/# Banyak kasino memiliki promosi untuk slot

  24. ThomasThype表示:

    Bermain slot bisa menjadi pengalaman sosial: slot 88 – slot 88

  25. DavidCooth表示:

    http://bonaslot.site/# Slot dengan bonus putaran gratis sangat populer

  26. Williecrady表示:

    Mesin slot sering diperbarui dengan game baru http://garuda888.top/# Mesin slot menawarkan pengalaman bermain yang cepat

  27. ThomasThype表示:

    Kasino memastikan keamanan para pemain dengan baik: bonaslot – bonaslot

  28. JamesLok表示:

    Slot dengan bonus putaran gratis sangat populer http://slot88.company/# Jackpot progresif menarik banyak pemain

  29. JamesLok表示:

    Slot dengan pembayaran tinggi selalu diminati https://slot88.company/# Pemain sering berbagi tips untuk menang

  30. JamesLok表示:

    Banyak kasino memiliki program loyalitas untuk pemain https://bonaslot.site/# Slot klasik tetap menjadi favorit banyak orang

發佈留言

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