1

I have this code:

document.getElementById('auth-button').addEventListener('click', authorize);

When my page load I want to trigger that without clicking the button.

This is my view

my View

When authorized button clicked this is the output

enter image description here

I want to auto click that button when my page load.

AyDee
  • 201
  • 1
  • 5
  • 13

7 Answers7

1

You can use addEventListener to the DOMContentLoaded event:

document.addEventListener('DOMContentLoaded', function() {
    authButton.click();
}, false);

Full example:

https://jsfiddle.net/7q0gxehk/1/

David Ng
  • 470
  • 4
  • 13
0

you can use Document ready in jQuery, try this..

$( document ).ready(function() {
    authorize();
});

or this in javaScript..

window.onload = authorize;

NOTE: The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.

Sarath
  • 2,318
  • 1
  • 12
  • 24
0

You could call the function authorize() on load of page using below code :

document.addEventListener("DOMContentLoaded", function(event) { 
    authorize();
});
shivgre
  • 1,163
  • 2
  • 13
  • 29
0

Use one of the following:

<body onload="script();"> 

or

document.onload = function ...

or

window.onload = function ...
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
Philb24
  • 67
  • 1
  • 9
0

You can register authorize as handler to be called when the page is fully loaded:

$(document).ready(authorize);

This requires jQuery. The same can be achieved without jQuery this way:

window.addEventListener('load', authorize);
mm759
  • 1,404
  • 1
  • 9
  • 7
0

It would be easier to tigger authorize function directly on page load using window.onload which is better than document.onload, see window.onload vs document.onload

window.onload = authorize;

However, if you are thinking about triggering click programmatically which is not suggested since it won't work properly across browsers e.g. Safari doesn't work at all

Community
  • 1
  • 1
Brian Liu
  • 331
  • 2
  • 6
0

None of the other answers offered thus far seem to take something into account - that the registered handler may in fact need to be aware of it's place in the DOM.

We could for instance, have a number of buttons that all call the same handler, with that handler manipulating the surrounding DOM. Simply calling authorize when the page loads will not be sufficient.

I've chosen to use DIVs instead of BUTTONs to demonstrate that the .click() method still works.

A far better way is to actually click the button, using javascript.

#1 Not working

function byId(id){return document.getElementById(id)}
function allByClass(clss){return document.getElementsByClassName(clss)}
// useful for HtmlCollection, NodeList, String types
function forEach(array, callback, scope){for (var i=0,n=array.length; i<n; i++)callback.call(scope, array[i], i, array);} // passes back stuff we need


window.addEventListener('load', onDocLoaded, false);

function onDocLoaded(evt)
{
 forEach(allByClass('mBtn'), addHandler);
 
 function addHandler(elem)
 {
  elem.addEventListener('click', authorize, false);
 }
 
 alert('hit a okay to call authorize');
 authorize();        // wont return from this call, since authorize relies on a valid 'this' value
}

function authorize(evt)
{
 this.classList.add('clicked');
 this.textContent = 'clicked';
}
.mBtn
{
 border: solid 1px #555;
 border-radius: 2px;
 display: inline-block;
}

.clicked
{
 color: #dddddd;
 pointer-events: none;
}
<div class='mBtn'>Try me</div><div id='btn2' class='mBtn'>Or me</div><div class='mBtn'>Or even, me</div>

#2 - Does work

function byId(id){return document.getElementById(id)}
function allByClass(clss){return document.getElementsByClassName(clss)}
// useful for HtmlCollection, NodeList, String types
function forEach(array, callback, scope){for (var i=0,n=array.length; i<n; i++)callback.call(scope, array[i], i, array);} // passes back stuff we need


window.addEventListener('load', onDocLoaded, false);

function onDocLoaded(evt)
{
 forEach(allByClass('mBtn'), addHandler);
 
 function addHandler(elem)
 {
  elem.addEventListener('click', authorize, false);
 }
 
 alert('hit okay to click the 2nd button with javascript');
 byId('btn2').click();        // will return from this call, since authorize relies on a valid 'this' value, and the btn gives it one.
}

function authorize(evt)
{
 this.classList.add('clicked');
 this.textContent = 'clicked';
}
.mBtn
{
 border: solid 1px #555;
 border-radius: 2px;
 display: inline-block;
}

.clicked
{
 color: #dddddd;
 pointer-events: none;
}
<div class='mBtn'>Try me</div><div id='btn2' class='mBtn'>Or me</div><div class='mBtn'>Or even, me</div>
enhzflep
  • 12,927
  • 2
  • 32
  • 51