Michael Delvege
Michael Delvege

Reputation: 19

Javascript on wordpress posts

I've been trying to use javascript on my wordpress post for over 2 hours now. I researched everything there is to research, and it still isn't working.

I've made sure to paste my code in the "text" tab of wordpress.

Can I get some help?

Here's my code:

<script language="Javascript">
<!-- 
              // Array of day names
              var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
                  "Thursday","Friday","Saturday");

              // Array of month Names
              var monthNames = new Array(
              "January","February","March","April","May","June","July",
              "August","September","October","November","December");

              var now = new Date();
              document.write(dayNames[now.getDay()] + ", " + 
              monthNames[now.getMonth()] + " " + 
              now.getDate() + ", " + now.getFullYear());

              // -->
</script>

Upvotes: 0

Views: 236

Answers (3)

Jon Koops
Jon Koops

Reputation: 9261

There are plenty of Wordpress plugins that provide that in a shortcode. For example: http://wordpress.org/plugins/extra-shortcodes, this also a lot less cumbersome than including a script every time you post.

Upvotes: 1

gwillie
gwillie

Reputation: 1899

what wp verion are you running. in wp 3.6.1, if you add js code in post editor, wp will add <p> tags around things in script. if you remove the comments <!-- blah -->, wp adds cdata around the script:

<script type="text/javascript">
//<![CDATA[
...code...
//]]>
</script>

but in frontend output it changes the last part of cdata //]]> to // ]]&gt;

i cant offer a solution as this would require an external js file. that being said, move js code to external file and all will be rosy

Upvotes: 0

Konsole
Konsole

Reputation: 3475

Get rid of all the white spaces and line breaks in your script. The WordPress is appending p tags to your code because of white spaces and line breaks and your code looks like this on execution:

<script language="Javascript">
<!-- 
              // Array of day names
              var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
                  "Thursday","Friday","Saturday");</p>
<p>              // Array of month Names
              var monthNames = new Array(
              "January","February","March","April","May","June","July",
              "August","September","October","November","December");</p>
<p>              var now = new Date();
              document.write(dayNames[now.getDay()] + ", " + 
              monthNames[now.getMonth()] + " " + 
              now.getDate() + ", " + now.getFullYear());</p>
<p>              // -->
</script>

Try this:

<script type="text/javascript">
<!--
  // Array of day names
  var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  // Array of month Names
  var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  var now = new Date();
  document.write(dayNames[now.getDay()] + ", " + 
  monthNames[now.getMonth()] + " " + 
  now.getDate() + ", " + now.getFullYear());
// -->
</script>

Upvotes: 1

Related Questions