WordPress: Fix ajaxurl Not Defined

I came across a situation where I needed to execute some ajax scripts from WordPress front end and I got an error: ajaxurl is not defined. As you may know, ajaxurl is a variable that holds the URL to WordPress ajax processing file, usually in the wp-admin section. In the past I’ve depended on other plugins defining it but in this case it was not. If you come across this issue, all you have to do is declare an action to wp_head and write a function to output the URL. This is also a good idea because you have a variable that may not be easily tempered with by other applications.

function f2b_ajaxurl() {
<script type="text/javascript">
var f2b_ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';