Show confirm only after page loading

Asked

Viewed 287 times

2

I am calling the following function inside the page:

window.onload = function() {
    acess();
}

function acess() {

    decision = confirm("Deseja realmente excluir o servidor cadastrado abaixo?");

    if (decision) {
        window.location.replace("fin.del.cad.php");
    } else {
        history.back();
    }
}

However, Confirm appears and the page is empty loading infinitely until I click on it.

I would like Confirm and/or Alert after page loading and not before. I tried some options I saw in other forums and it didn’t work out so well.

  • You can leave too window.onload = acess; and reverse order - leave access before onload.

  • Didn’t work :(

2 answers

2


You can give a late in the confirmation box by placing a setTimeout after the .onload. A minimum value of 1 second may be sufficient (I commented on the test redirects).

window.onload = function() {
   setTimeout("acess()",1000);
}

function acess() {

    decision = confirm("Deseja realmente excluir o servidor cadastrado abaixo?");

    if (decision) {
        // window.location.replace("fin.del.cad.php");
    } else {
        // history.back();
    }
}
Conteúdo da página
<br>
<img src="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg" width="100">

Using readyState

The readyState will return loading until the page is fully loaded. After returning complete i still give another 1 second to perform the confirmation:

var chkReadyState = setInterval(function() {
    if (document.readyState == "complete") {
        clearInterval(chkReadyState);
        setTimeout("acess()", 1000);
    }
}, 100);
  • Man, you’re very Dopper, you served right, thank you!

  • 1

    @Shinchila_matadora Thanks! I cloned in response another option. Abs!

1

THE ATTRIBUTE DEFER

The attribute defer tells the browser to run the script only when the HTML scan is finished.

<script defer src="script.js">

The script will be requested asynchronously, its download will be completed, and only when the parsing of the HTML document is finished will it be executed. Even if the full download of the script happens before the full HTML scan, it will only run after.

If you come to have multiple script elements with the Defer attribute.

<script defer src="script.js">
<script defer src=“jquery.js">
<script defer src=“bootstrap.js">

They will be requisitioned in parallel and executed in the declared sequence.

script js.

decision = confirm("Deseja realmente excluir o servidor cadastrado abaixo?");

if (decision) {
    window.location.replace("fin.del.cad.php");
} else {
    history.back();
}

Support for Defer in current browsers is great:

inserir a descrição da imagem aqui

Credits: ASYNCHRONOUS VS DEFERRED JAVASCRIPT

Browser other questions tagged

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