0
votes

I am calling the javascript function while from the HTML onClick event. I works fine when the page is loaded but when i click the button while the document is not ready it gives undefined function error in the console.

<a onclick="myfunc(params)" href="javascript:void(0)" >Click Me</a>

myfunc is defined in another js file.

1
try moving your script into the head - Ismail Farooq
@IsmailFarooq yes, but that can slow the page loading speed. - Mehar
i think there is no other way. May be you can cache your page that helps you to load page fast see here w3schools.com/html/html5_app_cache.asp - Ismail Farooq

1 Answers

0
votes

You need to call the javascript file that contains this function in the head section of the page. We load javascript files using the script tag and it's render blocking, which means that the required will be loaded first and then this Click Me will load and you can then click on this whenever this link is visisble.