john stoe john stoe - 2 months ago 12
HTML Question

Call a JavaScript function before declaration

I have this function inside

test.js
:

function testing(){
document.write("<p>Tes1</p>");
}


And then a basic HTML page.

Is it possible to call the function before being declared and add this to the page?

<p>Test1</p>


Because if I include the
test.js
at the bottom of the page, it’s not working. Is there a way to make it work (with a JavaScript file being included at the bottom of the HTML page)?

Answer

put your testing() function call inside window.onload callback, like this,

window.onload = function(){ testing(); }