Simulate Automatic Click when loading HTML page

Asked

Viewed 814 times

-1

I have this Pod, and I wish it to be invisible:

<input type="button" id="invisivel" value="" style="visibility: hidden;" onclick="toggleFullScreen()">

I would like when the page pressed that button to be automatically clicked.

I have this script, but it doesn’t work:

window.setTimeout(function(){
  document.getElementById("invisivel").click();
}, 2000);

Script of toggleFullScreen:

function toggleFullScreen() {
                    if ((document.fullScreenElement && document.fullScreenElement !== null) ||    
                     (!document.mozFullScreen && !document.webkitIsFullScreen)) {
                      if (document.documentElement.requestFullScreen) {  
                        document.documentElement.requestFullScreen();  
                      } else if (document.documentElement.mozRequestFullScreen) {  
                        document.documentElement.mozRequestFullScreen();  
                      } else if (document.documentElement.webkitRequestFullScreen) {  
                        document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);  
                      }  
                    } else {  
                      if (document.cancelFullScreen) {  
                        document.cancelFullScreen();  
                      } else if (document.mozCancelFullScreen) {  
                        document.mozCancelFullScreen();  
                      } else if (document.webkitCancelFullScreen) {  
                        document.webkitCancelFullScreen();  
                      }  
                    }  
                  }

From what I understand he clicks one click after 2 seconds, unfortunately it is not working. I would like when you press the page the button to be automatically clicked.

  • The button is already invisible when loading the page, what happens when executing the click event, is the execution of the function toggleFullScreen(), that you did not show in the content of the question.

  • Exactly that, because perform the function toggleFullScreen is working perfectly, my question is: simulate the automatic click of this buttonwhen loading the page.

  • @bfavaretto he said automatically, it assumes that he wants the fullscreen to be activated automatically, which is impossible and I’m also sure that would be annoying to the end user, as I replied in https://answall.com/a/306227/3635 ... I switched the dups

  • "index.html:19 Failed to execute 'requestFullscreen' on 'Element': API can only be initiated by a user Gesture." Google Chrome shows this on the console.

  • @Guilherme ok, this dup is even better

  • @Benilson translating the phrase, what the browser said was: "This API can only be initiated by a user gesture", ie only real user interactions work, simulate is impossible, the browser knows the difference, as I explained in my reply (linked above)

  • @Guilhermenascimento yes, I’m used to English, but sometimes I forget that others may not be, thank you for translating.

  • "mozRequestFullScreen() is obsolete. index.html:17:49 The full screen request was denied because Element.requestFullscreen() was not called from a short duration Event Handler created by a user." Firefox console, this already came in English.

Show 3 more comments

2 answers

0

Just call the click() javascript

To execute the call when loading the page, it must be called inside the event window.onload

Example:

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

The parameter elementName will be the id of your HTML code.

Test function:

function msgWelcome(){
    alert("Ei, seja bem vindo")
}

Call in your HTML:

<input type="button" id="elementName" value="" style="visibility: hidden;" onclick="msgWelcome()">

Test that way, because it’s supposed to work.

  • It didn’t work bro, the page loads but nothing happens.

  • So, you did not set an action for the button, the button is invisible and does not perform any other action, so it will not happen ANYTHING even, recommend to define better what you need.

  • But I did, it’s in context.

  • 1

    Look, onclick="toggleFullScreen()" is not set, what the function ``oggleFullScreen` does? you have her code ready? post it to us, edit your question and pass it.

  • I posted on the question.

  • Dear Wagner, this does not work, I recommend you read https://answall.com/a/306227/3635

Show 1 more comment

0

Try this, you can take the click event:

   document.onload=toggleFullScreen();
  • Nothing happened, I load the page and do not click.

  • @Expresso1002, I made a change in response, now it will work

  • I don’t understand, take the event click?

  • Dear Adjair this does not work, I recommend you read https://answall.com/a/306227/3635

  • Dear @Guilhermenascimento, I took the test and it worked normally.

  • @Adjaircosta entered fullscreen when loading the page? In which browser was this?

Show 1 more comment

Browser other questions tagged

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