-2

I was just experimenting with some javascript code on JsFiddle and then I was completely blank being not able to understand a code. JsFiddle link is http://jsfiddle.net/K3w7c/

Question:What does following mean?

window.onload=function(){
var clicked = function(){
    alert('i am hit');
}
}

and how to do I call the "clicked" function from a code something like

<a href="#" onclick="clicked();">Hit me</a>

please note: I know if the function definition was outside the window.onload then it will work but I am really not able to understand the use case when would I have a function declaration inside window.onload and if there is such a declaration then how do I call it.

thanks.

Prerak K
  • 10,940
  • 7
  • 30
  • 37
  • 1
    Learn about "scope" and "closures", see http://stackoverflow.com/questions/500431/javascript-variable-scope and http://stackoverflow.com/questions/111102/how-do-javascript-closures-work – elclanrs Feb 28 '14 at 06:06
  • I am really not sure what is the issue with the question , so many negative votes. it was a genuine question that I was not able to understand a piece of code. – Prerak K Feb 28 '14 at 06:09
  • It's pretty simple, may be that's why users are downvoting you for the lack of research efforts, it's a function which is invoked `onclick` event, that's it.. – Mr. Alien Feb 28 '14 at 06:10
  • http://jsfiddle.net/K3w7c/1/ – Mr. Alien Feb 28 '14 at 06:11
  • Thanks Mr. Alien at least you responded constructively. When you say "it's a function which is invoked onclick event" but I see a error on console when I hit on the anchor link. please can you elaborate further. – Prerak K Feb 28 '14 at 06:12
  • The question doesn't look too bad, I didn't downvote, but there are probably many duplicates. – elclanrs Feb 28 '14 at 06:14
  • @Mr.Alien please can you change the value on the top right drop down to "onload" on your jsfiddle link. – Prerak K Feb 28 '14 at 06:14

4 Answers4

2

to use clicked function you need to declare it on window, and if you want to declare clicked on load event, this approach can be used:

window.onload=function(){
    clicked = function(){
        alert('i am hit');
    }
}

clicked is defined on window by omitting the var keyword

Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
  • @amr1r_5h, any use case where in such approach is useful. i.e creating a function once window is loaded. – Prerak K Feb 28 '14 at 06:33
  • 1
    declaring `event handlers` in `load` event is not common, because these declaration doesn't need to any asset be loaded before, `onload` is used when you need to do an operation when all assets has loaded – Amir Sherafatian Feb 28 '14 at 06:40
1

The var clicked is a local variable not a global variable, so you can't visit it in the tag.

JackYe
  • 122
  • 1
  • 2
  • 13
0

You have to make the clicked function global like;

window.onload=function(){
  clicked = function(){
    alert('i am hit');
}
}

Just remove the var. That will do.

BKM
  • 6,949
  • 7
  • 30
  • 45
-1

onload is an event which fires when an object has finished loading.

It's usually used to ensure everything is ready before running javascript.

A common use case is when you are running code which uses external libraries which need to be fetched:

window.onload(function(){ 
 thirdPartyLib.init();
});
ioseph
  • 1,887
  • 20
  • 25