If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the D
And if you want to run a second function after the first one finishes, see this stackoverflow answer.