How to get this to load last?
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("div.domTip_tipBody").mouseover(function(){
jQuery("#yyy a.tippy_link").css("background-color","yellow");}); Â
jQuery("div.domTip_tipBody").mouseout(function(){
jQuery("#yyy a.tippy_link").css("background-color","red");
});
}); Â
</script>
It is running before “div.domTip_tipBody” is being run (which is generated by a shortcode) so nothing happens.
I have tried:
<script src="script.js" type="text/javascript" defer="defer"></script>
in my header.php before
</head>
but it doesn’t help.
Have seen “window.onload =” but not sure how to apply it.
Any insight is appreciated.
Thanks
your_name.js
.Add the following to your
functions.php
file:This looks like a jQuery event delegation issue. If the new DOM elements are added after the page has loaded, jQuery wont read them. So you need to tell jQuery before hand to bind an event if any such element exists now or in future.
try
$.delegate('div.domTip_tipBody','mouseover', function(e){
jQuery("#yyy a.tippy_link").css("background-color","yellow");});
});
See http://api.jquery.com/delegate/ , http://lab.distilldesign.com/event-delegation/
You can simply add the script to footer.php and it will probably work, but it’s not the best way to avoid jQuery conflicts.
The best way is to enqueue the script from a separate file. See http://codex.wordpress.org/Function_Reference/wp_enqueue_script which has a parameter to load scripts in footer via wp_footer .
And see The Developerâs Guide To Conflict-Free JavaScript And CSS In WordPress: http://wp.smashingmagazine.com/2011/10/12/developers-guide-conflict-free-javascript-css-wordpress/
This might be a ‘little’ late, but just in case anybody is still looking:
http://css-tricks.com/snippets/jquery/run-javascript-only-after-entire-page-has-loaded/
Nice and simple way of doing it!