Open window 1 only

1

I want to click on the page a window opens and I am using this code

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

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

But it opens every time that you clicked on the window and wanted it to run only once, and then it would only run if the window was reloaded

    
asked by anonymous 02.09.2017 / 16:19

2 answers

1

Place 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.br", "_blank", 
       "toolbar=yes, scrollbars=yes, resizable=yes, top=1, left=1, width=1, height=1");
        }
        status = 1;
    }

</script>

or up to documento.onClick to null after first run:

<script>

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

    function myFunction() 
    {
        window.open("http://google.com.br", "_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

    
02.09.2017 / 16:23
1

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

    
02.09.2017 / 16:23