Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use a function inside "windows.onload" outside of curly braces?

is there a way to use a function written inside of windows.onload outside it’s curly?

I mean to make it callable somehow without the need to rewrite the entire same function outside?

For instance:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   window.onload = function () {
     async function doSomething() {
     // do something...
     }
   doSomething(); // I want to use this function outside windows.onload "without" rewriting the exact same function outside.
   }
   // make doSomething() callable here somehow. 

Is is possible with Vanilla Javascript or will I need to rewrite the same function again?

>Solution :

No.

You could assign doSomething to a global variable inside onload but it would be assigned too late to use it where you want to.

If you want doSomething to be a global, then define it as one in the first place. Don’t scope it to the onload function.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading