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.

4
  • authorize();. Commented Oct 3, 2016 at 9:55
  • Duplicate Commented Oct 3, 2016 at 9:55
  • @melpomene - useless if the handler relies on this, or it's(the button's) place in the DOM tree. Not a helpful suggestion imho. Commented Oct 3, 2016 at 9:57
  • AyDee, I see that you are using vanilla javascript so I have posted my answer accordingly. Could you check if that helps. Commented Oct 3, 2016 at 10:00

7 Answers 7

1

You can use addEventListener to the DOMContentLoaded event:

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

Full example:

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

OP has not mentioned that he is using jQuery, solution should be in javascript.
0

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

document.addEventListener("DOMContentLoaded", function(event) { 
    authorize();
});

Comments

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

Comments

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

Comments

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>

Comments

0

Use one of the following:

<body onload="script();"> 

or

document.onload = function ...

or

window.onload = function ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.