Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put my javascript in the footer

I just want to ask on how to print script 'javascript' at the footer using simple plugin. I'm using WordPress 3.0 any ideas?

like image 893
Trez Avatar asked Nov 19 '10 03:11

Trez


People also ask

Can you put JavaScript in the footer?

Javascript should be placed in footer of html document wherever possible. For libraries like jQuery we don't often have a choice as there may be some other javascript code which depends on that. But we should design site in such a way that maximum javascript code is placed in the end.

How do I move JavaScript to the bottom?

Lets assume that your plugin or theme is adding raw JavaScript in the header or between the content. Find the raw JavaScript code in the plugin or theme files, copy the JavaScript and save it in a . js file. Then use wp_register_script() function as shown above, to move JavaScript to the bottom.


2 Answers

Use a functions.php file inside your theme template add this :

<?php  function add_this_script_footer(){ ?>  [YOUR JS CODE HERE]  <?php }   add_action('wp_footer', 'add_this_script_footer'); ?> 

Hope it helps!

like image 102
Jk_ Avatar answered Oct 09 '22 23:10

Jk_


For an external javascript file to be linked in the footer, use this (>= WP2.8)

function my_javascripts() {     wp_enqueue_script( 'the-script-handle',                         'path/to/file.js',                         array( 'jquery','other_script_that_we_depend_on' ),                         'scriptversion eg. 1.0',                         true); } add_action( 'wp_enqueue_scripts', 'my_javascripts' ); 

That last true means that the script should be put at the wp_footer() hook.

like image 28
windyjonas Avatar answered Oct 09 '22 22:10

windyjonas