How to add javascript to page

Hi, I’m trying to add this simple javascript to my page to display the current year. It works but it makes Cornerstone not work. When I put it in cornerstone disappears and all you see is 2020. I tried using the JS customizer but then the script wont load. How can I make this work?
Thanks
Rena

Today’s year is unknown

Hi @fatcatgraphics,

Normally the JS code should work without any issues when you place it in the Global JS panel or a page JS in Cornerstone as long as there are no errors in the code.

Would you mind posting the code you are trying to use so that we can check it?

Thank you.

Sure, here it is. I ran the code on the page and when I saved it and looked at the front end it does work. The problem is that I cant use cornerstone bc it disappears and only displays the year.

I have this paragraph and the word unknown is wrapped in a span tag with id newYear.

Today's year is unknown

var newYear = document.write(new Date().getFullYear());
var span = document.getElementById(‘newYear’);
span.innerHTML = newYear;

Hi @fatcatgraphics,

Instead of adding the code in the Global JS, Please install and activate the child theme and login through FTP then edit the functions.php then add this code:

add_action( 'wp_footer', 'adding_scripts_to_footer', 10 );

function adding_scripts_to_footer() { ?>
 <script>
   var newYear = document.write(new Date().getFullYear());
   var span = document.getElementById('newYear');
   span.innerHTML = newYear;
 </script>   
<?php }

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.