jQuery window on load is firing too early. What else can I use? Javascript in Wordpress

I'm learning jQuery and Javascript, and I'm trying to understand why my code is not working.

I'm doing this from inside a custom plugin in WordPress.

My javascript file with the function I'm trying to call is enqueued in the WordPress footer:

<script type='text/javascript' src='..... gf_timer.js?ver=1.1.0.1'></script>

and my PHP is generating items like the following:

<div id="gf_timer_1">Placeholder</div>
<script type="text/javascript">$j=jQuery.noConflict(); $j(window).on('load',gfInitTimer(1));</script>

I just want to call the function gfInitTimer() after each div. I keep on getting an error "gfInitTimer is not defined".

So I added the onclick handler as a test and that's finding the function properly:

<div id="gf_timer_1" onclick="gfInitTimer(1)">Placeholder</div>
<script type="text/javascript">$j=jQuery.noConflict(); $j(window).on('load',gfInitTimer(1));</script>

I thought the window.on('load', should fire after the page and all scripts are loaded.

What am I missing?

#javascript #php #jquery #wordpress

4 Likes2.75 GEEK