How to open a new pop-up window by clicking anywhere on the page?

Asked

Viewed 2,600 times

0

I’m willing to make sure after the visitor click anywhere on my page to open a pop-up window behind the main window the visitor is in. So he will not see the window that has been opened.

I’m using this function:

window.onload = function()
{
    document.onclick = function( e )
    {
        window.open( 'http://www.google.com' );
    }
}

The problem with this function above is that it only opens a new tab.

I would like it to open as this function below:

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

But with this function above it only opens if there is a button to click. I wanted him to open up when you click anywhere on the page. But behind the main window.

  • do you want to do something without the user knowing? it doesn’t seem ethical.

  • Good evening Seth, do not use irrelevant tags, the tags should refer to the problem, ajaxe Node.js has nothing to do with the situation. I recommend you read: http://answall.com/help/tagging - I’m sure you’ll take this comment as a constructive criticism :)

  • I am willing to do this for a video portal of mine, as I will not have sponsors, I will be using ADS systems so that I can earn my money. then this was the only way I found for all visitors who enter generate a click for me in the ads without them knowing, I do not want to harm anyone and the window will only be opened once only then will not open more.

  • Sorry friends but I do not know much the rules of the site. I am very focused on being able to solve this doubt that I am. can you help me?

  • 1

    I hope that the developers of browser’s add-ons see this question/answer and find blocking for such clique ... sem eles saberem. Hideous piece. But, +1 for the well-asked question.

1 answer

1

Put the function outside the window.onload that will work.

Example:

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");
}

See working on Jsfiddle

window.onload is halted only after all html,css,images files have been loaded, now if you only need HTML(DOM) you can use without or putting one $(document).ready().

  • 1

    I’m sorry Gabriel, but I need to comment on your reply, the addEventListener was not what solved the problem, the only thing that did was to exchange onclick for addeventlistener, what solved the problem of AP was to put myFunction inside the event, personally I think that this should be the focus of the response. I’m sure you’ll take mine as a constructive criticism :)

  • Of course I take as a constructive criticism, the #SO is for this, I performed some tests and using Document.onclick will work the same way, the difference here, was in window.onload, which I realized does not load in time to be executed

  • For me what matters is that he is helping the boy to solve the problem, I think knowledge not one should have criticism and yes questioning and response, Gabriel Voce did well answered how everyone should respond and not keep saying to this is not ethical.

  • Gabriel I support Cvoce, what matters is that Cvoce is helping friends with doubt to remove doubts and does not criticize with what is ethical. the important thing is that knowledge is never too much.

  • Gabriel I am very grateful for your help, You already helped me solve what I was able to do. Now I just need to get this little window to open back in the main window

  • Guilheme thanks also for the help but what is missing for me to make this window open through the main window :D

  • William Thank you so much for triggering the questioning of Grabriel so that we can better always more and more.

  • The OR proposal is to help with ethics. It is very clear what you are trying to do, if the user enters your site, you want to make money by brute force. Each one enters the site you want.

Show 3 more comments

Browser other questions tagged

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