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

17,046 Responses

  1. Charlesbot表示:

    https://fastpillseasy.com/# ed meds by mail

  2. Donaldabula表示:

    Buy Tadalafil 5mg MaxPillsForMen.com Generic Cialis without a doctor prescription

  3. RogerFaf表示:

    http://maxpillsformen.com/# Buy Tadalafil 20mg

  4. BrettTIT表示:

    buy Viagra over the counter: FastPillsForMen – generic sildenafil

  5. RogerFaf表示:

    https://fastpillsformen.com/# Generic Viagra online

  6. New Jersey College Listing for the Bordentown Regional School District, New Jersey Department of Education.

  7. отели нижнего новгорода [url=https://otelinizhniinovgorod-ru.ru]отели нижнего новгорода[/url] .

  8. After prolonged deliberation I decide on the bishop transfer.

  9. Donaldabula表示:

    Generic Cialis price MaxPillsForMen.com Tadalafil Tablet

  10. Reduce a chunk of ribbon long enough to go round kid’s neck, plus eight inches.

  11. The Meyer Group is part of Asia Wealth Group Holdings which is a publicly traded firm listed on the PLUS Stock alternate in London UK.

  12. вывод из запоя в ростове на дону [url=vyvod-iz-zapoya-rostov236.ru]вывод из запоя в ростове на дону[/url] .

  13. наркология вывод из запоя ростов наркология вывод из запоя ростов .

  14. BrettTIT表示:

    Order Viagra 50 mg online: FastPillsForMen – Sildenafil 100mg price

  15. Baginda168表示:

    Your idea is outstanding; the issue is something that not enough persons are speaking intelligently about. I’m very happy that I stumbled throughout this in my seek for one thing regarding this.

  16. These who’re concerned in futures trading is known as as future traders.

  17. There is a conflict of interest in the RBI managing both the roles.

  18. Donaldabula表示:

    buy cialis pill Cialis 20mg price in USA Buy Tadalafil 20mg

  19. Barrybib表示:

    Viagra tablet online: buy viagra online – Sildenafil Citrate Tablets 100mg

  20. Baginda168表示:

    I’d like to be able to write like this, but taking the time and developing articles is hard…. Takes a lot of effort.

  21. RogerFaf表示:

    http://fastpillsformen.com/# Sildenafil 100mg price

  22. gtbike.ru [url=http://newsfast.ru/turnikety-tripody-chto-eto-i-pochemu-oni-neobxodimy//]http://newsfast.ru/turnikety-tripody-chto-eto-i-pochemu-oni-neobxodimy//[/url] .

  23. RogerFaf表示:

    http://maxpillsformen.com/# Generic Tadalafil 20mg price

發佈留言

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