Adding Custom Javascript to Thesis

by Melvin Ram

Being able to add custom javascript to your website can help you do lots of cool things. I’ll save all the cool things for now and simply demonstrate how to add the custom javascript and do a simple thing. In later posts we can dig into the cool things you can do using this technique.

Add the following code to your custom_functions.php file inside your custom folder:

function add_custom_javascript(){ ?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/custom/custom.js"></script>
<?php
}

add_action('wp_head', 'add_custom_javascript');

Then create a text file called custom.js inside your custom folder and add the following:

// This will look for all links with rel="external"
// and cause the link to open in a new window.
$(document).ready( function() {

 $('A[rel="external"]').click( function() {
 window.open( $(this).attr('href') );
 return false;
 });

});

As the comment above the indicated, this javascript code will look for all links with the rel attribute set to external and open them in a new window. Why do it using javascript and not just the option that WordPress has built in? Because the _target property which the WordPress approach doesn’t validate.

Leave a Comment

Spam Protection by WP-SpamFree

Previous post:

Next post: