Code to open new tab link with counter

Asked

Viewed 46 times

0

I have no experience with programming, I would like a help: I found this code on the internet:

    <html>
<head>
<script type="text/javascript">
var contador = 20;
function contar() {
    document.getElementById('contador').innerHTML = contador;
    contador--;
}
function redirecionar() {
    contar();
    if (contador == 0) {
        document.location.href = 'https://google.com';
    }
}
setInterval(redirecionar, 1000);
</script>
</head>
<body>
<p>Link vai expirar em... <label id="contador"></label></p>
</body>
</html>

but this code does the function of redirecting to the informed link after a given time in the counter.

I would like a code that opens the informed link in a new tab, keeping the current tab open, after a certain time in the counter. Thanks in advance

  • Please edit the question to limit it to a specific problem with sufficient detail to identify an appropriate answer.

  • Opening a new tab or window without user action goes against the security guidelines of browsers and they block this scheme.

3 answers

2

-1

To make it very simple for you, who has no contact with programming. Just change the value of the variable your_link to the link you want to open in a new tab.

<html>
<head>
<script type="text/javascript">
var seu_link = "https://google.com/"
var contador = 20;
function contar() {
    document.getElementById('contador').innerHTML = contador;
    contador--;
}
function redirecionar() {
    contar();
    if (contador == 0) {
        window.open(seu_link);
    }
}
setInterval(redirecionar, 1000);
</script>
</head>
<body>
<p>Link vai expirar em... <label id="contador"></label></p>
</body>
</html>
  • Boy, top top little brother top... worked just the way I wanted it. send me your Pix so I can send you a thank you

-2

A simple error that I noticed in this code, is that the counter does not stop when it reaches zero. And it continues printing on screen -1, -2, -3... Infinitely.

Your problem has already been solved by colleagues, so I will quote only the part of the accountant.

Create a condition for your role contar() since it is called by the redirect function, every 1 second. A simple if checking whether the contador é maior ou igual a zero already solves, because after reaching zero will be redirected, but the function would still call the contar(), but as the counter would be less than zero, then it no longer falls into your if and no longer prints on screen.

Follows modified function:

function contar() {
    if (contador >= 0) {
        document.getElementById('contador').innerHTML = contador; 
        contador--;
    }
}

Complete code:

<html>
<head>
<script type="text/javascript">
    var seu_link = "https://google.com/"
    var contador = 20;
    function contar() {
        if (contador >= 0) {
            document.getElementById('contador').innerHTML = contador; 
            contador--;
        }
    }
    function redirecionar() {
        contar();
        if (contador == 0) {
            window.open(seu_link);
     }
    }
    setInterval(redirecionar, 1000);
</script>
</head>
<body>
    <p>Link vai expirar em... <label id="contador"></label></p>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.