1
How do I stop when the user clicks anywhere on my website to open a determined external site in a new tab and with cookies to limit the opening of the site determined by time?
1
How do I stop when the user clicks anywhere on my website to open a determined external site in a new tab and with cookies to limit the opening of the site determined by time?
7
In short it would be this:
<script>
(function () {
var isOpened = false;
//Troque aqui pelo site desejado
var siteUrl = "http://www.google.com";
document.addEventListener("click", function(){
if (!isOpened) {
isOpened = !!window.open(siteUrl, "_blank");
}
});
})();
</script>
Add the click event to document.
, use window.open
without window configuration parameters, I created a variable isOpened
to check if the pop-up has already been opened, if it has been clicked then ignore the next clicks (you can also use document.removeEventListener
)
Note [1]: I added the
!!
in front of thewindow.open
because some popup blocking plugins block up calls coming from clicks, thenwindow.open
returnsnull
, so you will know whether the popup has been opened or not.Note [2]: I used an anonymous function to isolate the variables from the global scope, thus avoiding accidents.
AP asked me for extra functionality, I am separated from the context above because the code above I believe will help people in specific situations, in this case AP wants to limit every 24 hours a click, ie opened the pop-up once only in 24 hours will occur again, even if you update the page.
This can be solved using cookies, for example:
<script>
//Função pra verificar se a janela foi já aberta hoje
function checkOpenPopupToday()
{
return /(^|;\s)isopenedpopup=1(;|$)/.test(document.cookie);
}
//Função pra definir em horas que a janela foi aberta
function setOpenPopupToday(hours)
{
var date = new Date();
date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
document.cookie = "isopenedpopup=1; expires=" + date.toGMTString() + "; path=/";
}
(function () {
//Troque aqui pelo site desejado
var siteUrl = "http://www.google.com";
//Troque aqui pelo tempo em horas desejado para impedir que o popup abra neste intervalo
var hours = 24;
document.addEventListener("click", function()
{
//Verificar pelo cookie
if (!checkOpenPopupToday())
{
//Verificar se o popup abriu com o clique
if (!!window.open(siteUrl, "_blank"))
{
//Se a janela foi aberta define o tempo para bloquear novas aberturas
setOpenPopupToday(hours);
}
}
});
})();
</script>
Thank you was right!
One more thing if you want to limit 1 click to every 24 hours as it would be?
@You can use a logic using cookies.
@I edited the answer with an example using cookies
@Guilhermenascimento ok thanks I will edit the question
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
target='_blank'
- you already tested that?– Sergio
Denis the William added "anywhere on the page" in the description of the question. Is that what you want? or is it in links
<a>
on the page?– Sergio
@Sergio I did not add the description of the question, I copied the content of the question and put in the title, are the words of AP itself =)
– Guilherme Nascimento
@Guilhermenascimento yes, true. But I still haven’t figured out if he has anchors or if he wants to do everything with Javascript.
– Sergio
@Guilhermenascimento found that the answer here is more complete. Denis, it is a good idea to clarify questions in the comments. For the sake of the question, your learning and also the kind of communication you want to have with the community.
– Sergio