1

On one of my previous pages I use

 <script src="/js/wireEvent.js"></script> 
<body onload="wireEvent();" >

Basically, wireEvent() displays an image on top of the page whenever the mouse goes out of the browser. However, now I cannot add this function directly to the body of my page, so what is the proper way to add it? Will it be right if I add

<script>
 window.onload = wireEvent;
</script> 

or I enclose the whole function in

 $( document ).ready 

and only include the function's file to my html?

Dimentica
  • 795
  • 2
  • 11
  • 30
  • 1
    Enclosing the function in `ready()` is not required. Don't use inline handlers, use `window.onload = wireEvent;` in ` – Tushar Oct 06 '16 at 08:44
  • 2
    `$( document ).ready` is a jquery implementation, `window.load` is native javascript implementation and `body onload` is inline event handler. So what is the requirement? – brk Oct 06 '16 at 08:45
  • Finally I used , I was not sure what is the difference and what are the best practices.Thanks! – Dimentica Oct 06 '16 at 08:47
  • 3
    Possible duplicate of [jQuery equivalent of body onLoad](http://stackoverflow.com/questions/4638296/jquery-equivalent-of-body-onload) – Endless Oct 06 '16 at 08:52

2 Answers2

3

The code below executes only after all your images load completely. So you can try this:

$(document).on('load', function(){
   wireEvent();
});
Dhurba Baral
  • 559
  • 5
  • 12
1

You can just do:

$(function(){ 
//jQuery code here 
});
Digant C Kasundra
  • 1,606
  • 2
  • 17
  • 27