There are several issues with your code. is_front_page()
is a PHP function, you need to call it within <?php ?>
.
The script need to be printed in HTML in order to make it work. Try this..
function wpse_363853_run_on_front() {
?>
<script>
window.addEventListener("load", function() {
<?php if( is_front_page() ){ ?>
console.log("front page");
<?php } ?>
});
</script>
<?php
}
add_action( 'wp_head', 'wpse_363853_run_on_front', 99 );
Just a note, avoid using anonymous functions.