Open window 1 single time

Asked

Viewed 50 times

2

I’m hoping that by clicking on the page a window opens and I’m using this code

 <script>
document.onclick = function( e ){
 myFunction();
}

function myFunction() {
window.open("http://google.com", "_blank", "toolbar=yes, scrollbars=yes, resizable=yes, top=1, left=1, width=1, height=1");
}
</script>

But it opens every time and clicked on the window wanted it to run a single time and then comes back to run only if the window was reloaded

2 answers

1

This is simple to declare a variable at the beginning type var click = false and when the person clicks you put this variable to true and put a if statement to check if the variable is false, if it is, open the window. And when reloading the page the variable will go back to false.

1


Put a control variable with the name of status, example:

<script>

    var status = 0;
    document.onclick = function( e )
    {
        myFunction();        
    }

    function myFunction() 
    {
        if (status == 0) 
        {
            window.open("http://google.com", "_blank", 
       "toolbar=yes, scrollbars=yes, resizable=yes, top=1, left=1, width=1, height=1");
        }
        status = 1;
    }

</script>

or even in the documento.onClick arrow to null after the first execution:

<script>

    document.onclick = function(e) 
    {
        myFunction();   
    }

    function myFunction() 
    {
        window.open("http://google.com", "_blank",
    "toolbar=yes, scrollbars=yes, resizable=yes, top=1, left=1, width=1, height=1");

       document.onclick = null; // anulando na próxima execução.
    }

</script>

Reference: Globaleventhandlers.onclick

Browser other questions tagged

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