First, I would comment all that JavaScript out and simply do a console.log('This Function is Running...')
to see if it is actually firing or not.
That being said, how I would approach this is to actually put this in a JavaScript file and simply enqueue it along with the rest of my styles/scripts. You can even have it conditionally load on the specific page it’s bring used on. So, using the enqueue function in _s as an example:
/**
* Enqueue scripts and styles.
*/
function _s_scripts() {
wp_enqueue_style( '_s-style', get_stylesheet_uri() );
wp_enqueue_script( '_s-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20151215', true );
wp_enqueue_script( '_s-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
if ( is_page( 'contact-me' ) ) {
wp_enqueue_script( 'cf7-tracker', get_template_directory_uri() . '/js/cf7tracker.js', array(), '', true );
}
}
add_action( 'wp_enqueue_scripts', '_s_scripts' );