WPDD

WordPress Develop & Design

Popup message with cookie

<div id="popupOverlay"></div>
<div id="popupMessage">
    <h3>Dear Portal User,</h3>
    <p>In recent days, our service portal has been unavailable due to technical reasons.</p>
    <p>The portal is back online today, but over the next 24 hours, you may experience disruptions and temporary access issues.</p>
    <p>We apologize for the inconvenience and are making every effort to restore full service as soon as possible.</p>
    <p>Thank you for your understanding.</p>
    <button id="closePopup">Close</button>
</div>
<style>
	/* Basic styling for the popup */
	#popupMessage {
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		background-color: #fff;
		padding: 20px;
		box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
		z-index: 9999;
		display: none;
		width: 300px;
		border-radius: 10px;
	}

	#popupMessage h3 {
		margin-top: 0;
	}
	#popupMessage p {
font-size: 0.9rem;
}
	#popupMessage button {
		display: block;
		margin: 20px auto 0;
		padding: 10px 20px;
		background-color: #007bff;
		color: white;
		border: none;
		border-radius: 5px;
		cursor: pointer;
	}

	#popupMessage button:hover {
		background-color: #0056b3;
	}

	#popupOverlay {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 9998;
		display: none;
	}
</style>

<script>
    function hasOneDayPassed() {
        const lastVisit = localStorage.getItem('service-polytex-lastVisit-1');
        const now = new Date().getTime();
        const oneDay = 24 * 60 * 60 * 1000;

        // Check if 24 hours have passed since the last visit
        if (!lastVisit || now - lastVisit > oneDay) {
            localStorage.setItem('service-polytex-lastVisit-1', now);
            return true;
        }
        return false;
    }

    function showPopup() {
        const popup = document.getElementById('popupMessage');
        const overlay = document.getElementById('popupOverlay');
        popup.style.display = 'block';
        overlay.style.display = 'block';
    }

    function closePopup() {
        const popup = document.getElementById('popupMessage');
        const overlay = document.getElementById('popupOverlay');
        popup.style.display = 'none';
        overlay.style.display = 'none';
    }

	window.addEventListener('load', function () {
		console.log('window.onload');
        if (hasOneDayPassed()) {
            showPopup();
        }

        document.getElementById('closePopup').addEventListener('click', function () {
            closePopup();
        });
    });
</script>