Script wont work when passed to wp_enqueue_script

After realizing i need to use the wp_enqueue_script for any javascript on my website, I have begun trying to use the wp_enqueue_script and am getting no where.

my .php file contains:

<?php
function EnactScript(){
    wp_register_script('CommercePlugin', plugins_url('js/CouponGenerator.js', __FILE__), array('jquery'), '', false);
    wp_enqueue_script('CommercePlugin');
}
add_action('wp_enqueue_scripts', 'EnactScript');
function GenCoupon(){
echo "<input type = "button" onclick = "randomCoupGen(); this.disabled = true">Click here to see if you won!!</input>";
echo '<imgr src="' . plugins_url('js/CouponGenerator.js', __FILE__) . '" > ';
}
add_filter('woocommerce_before_checkout_form', 'GenCoupon', 9999999, 2);
?>

This it the CouponGenerator.js code:

<html>
<head>
<script type = "text/javascript">
function randomCoupGen()
{
var RanNum = Math.floor(Math.random() * 6);
if(RanNum == 1)
{
alert("You rolled a 1");
}
else if(RanNum == 2)
{
alert("You rolled a 2.");
}
else
{
alert("You rolled something other than 1 or 2");
}
}
</script></head><body></body></html>

What do I need to do to make sure my JS is getting loaded when wp_head(); is called?

Read more here: Script wont work when passed to wp_enqueue_script

Leave a Reply

Your email address will not be published. Required fields are marked *