Simulate click when opening the page

Asked

Viewed 8,495 times

4

There is way, when someone enters a website, a Javascript event "simulate" a click? In other words, Javascript click on the site automatically as soon as the person enters.

In case I have an advertising ad that only opens when someone clicks on the page. That is to click on the page to open a new advertising page.

<script data-cfasync="false" type="text/javascript" src="http://www.liveadexchanger.com/a/display.php?r=961387"></script>

In case there would be way, hardly any user enters, open this page simulating a "click". 'Cause I’ve tried every way I’ve been told.

  • 1

    document.getElementById("meuElemento").click()

  • Instead of my element, I put what? A div for example?

  • I tried to apply, but I couldn’t.

  • I replied in more detail to you.

  • It would be when clicking on any part of the page trigger another click event?

  • No, in case just seeing would open that javascript simulating an event.

  • @Gonçalo, by chance, what you want is not to get control of the user’s mouse cursor, which would go to a certain part of the page and click alone on an element (a div for example). 'Cause if it is, I don’t think it will, it would be a security breach...

Show 2 more comments

3 answers

3

Just call the click() an element. Example:

document.getElementById("meuElemento").click();

Where meuElemento is the id of any element in html, as long as it has an event click, such as elements input, button, a, div etc..

For it to be called when loading the page must be inside the event window.onload:

window.onload = function()
{
    document.getElementById("meuElemento").click();
}

Fiddle 1, Fiddle 2

  • I couldn’t do what I wanted, but I edited my answer to give more details.

  • @Gonçalo edited the answer or the question?

  • The question I apologize.

2

Do the following:

In Javascript:

function teste(){
   alert("Teste")
}
window.onload = function() {
   document.getElementById("elementoTeste").click();
}

In html:

<input type="button" onclick="teste()" id="elementoTeste">
  • I couldn’t do what I wanted, but I edited my answer to give more details.

  • The question I apologize

  • @Deesouza should work with the example I gave. Can you include your source in the question too? Make sure the element id is right

  • @Renan never mind, it doesn’t matter to me what other people post in their answers.

  • @Is Deesouza going to answer the question or are you just going to keep causing it? Helping to turn this into a forum or yahoo answers even more than it already is ? It’s full of article around against HTML inline event, just search...

  • @Dontvotemedown what I did ? I’m here in good. Folks already answered.

  • @Deesouza de boa djow.

  • Last use case trigger().

  • @Deesouza good, only that trigger() is jQuery.

  • Yeah, that’s why I said last time.

Show 5 more comments

0

For me I managed to make it work this way. I implemented the automatic click in 5000 mls and then closed the loop after 1000 mls. Thus, only 1 automatic click.

<script> var myVar = setInterval(function ({document.getElementById("teste").click();}, 500); setInterval(function () {clearInterval(myVar)}, 1000); </script>

Browser other questions tagged

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