In order for us to be able to read your code easily and respond to your question, it helps if your code is formatted properly. If it isn’t, sometimes some of it will not display at all.
I have formatted your code for you this time, but in the future you can do one of the following:
Highlight your code and then select the </> icon from the top of the edit window.
OR
Place three backticks (`) on the line just before your code, and three more backticks on the line after the code.
Well if you want your visitors to leave instantly, that can reliably be achieved by setting #close to display: none; as well, and then adding an event listener to the #accept button which would show the close button like
var acceptButton = document.getElementById('accept')
var closeButton = document.getElementById('close')
acceptButton.addEventListener('click', function () {
closeButton.style.display = 'block'
})
Since there is no backend interaction involved, I’d suggest to use the session storage instead, which will expire automatically when the user leaves the site. The timeout part might then become
var modalBox = document.getElementById('modal-box')
var isActiveSession = window.sessionStorage.isActiveSession
if (!isActiveSession) {
window.sessionStorage.isActiveSession = 'true'
window.setTimeout(function () {
modalBox.style.display = 'block'
}, 30000)
}
If you do need a cookie however, the approach would be analogous.
thanks… i have worked on the click button last night but the delay is working now, thanks to you,
just a question this part of code will be installed/ embedded into a main site wich will be 400+ pages… for the cookie, will the code work for that scenario?
I’ve been using the session storage instead of a cookie above, but yes that would work across all pages of your site. Check out the link for more information!
Do you mean half the input elements being filled or checked? I suppose you might do something like
var form = document.getElementById('my-form')
var submit = document.getElementById('submit')
form.addEventListener('change', function () {
// Get the elements of the form that have a value and are
// not the submit button
var filled = Array.from(form.elements).filter(function (el) {
return el.value !== '' && el.type !== 'submit'
})
// Set the disbaled-state of the button depending on whether
// at least half of the elements are filled
submit.disabled = filled.length < form.elements.length / 2
})
… but I’d think that could be a bit confusing for the user… better just make required fields required and mark them as such. You could then just use the required HTML attribute here.
yea i figured… ok what needs to be done now is make the modal show once every session… i dont know how to make it so it does it… like what needs to be done is even if the page is reloaded/ refreshed, the modal wont show if it was already appeared once
the delay works, but when you refresh the page, it shows it again… what i need is when it showed already, it will only come back when the user exits the tab and make a new session
Again, it works for me in the fiddle from my post #11. If this doesn’t work for you, you probably have cookies and web storage disabled in your browser. If my fiddle works but only on your page it does not, there’s probably another bug in your code; in this case, I can only help you if you provide a link to that page, or a fiddle that reproduces the problem (or at least the relevant code).
i did checked the fiddle… it’s probably the cookies on my browser then, cos what im using is the exact same code on what I have posted. Thanks for your help… i will shoot a question if i dont fix it
Ah yes, that helps. Well you need to check the console for errors – isActiveSession is not defined. ;-) You forgot this part:
var isActiveSession = window.sessionStorage.isActiveSession
Also, you’re setting the timeout outside of the if (isActiveSession) {} check, so of course it would have no effect. It works with my snippet from post #4:
var modalBox = document.getElementById('modal-box')
var isActiveSession = window.sessionStorage.isActiveSession
if (!isActiveSession) {
window.sessionStorage.isActiveSession = 'true'
window.setTimeout(function () {
modalBox.style.display = 'block'
}, 30000)
}
No that should make no difference at all. Are there maybe any errors in the console? If not, I can only suggest you carefully review the code for logic errors, and debug it by setting breakpoints, logging to the console etc… like, does it run into the conditional? And if not, what is the value of isActiveSession at this point?