0

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>
19
  • Why not just do this in PHP? Commented Oct 3, 2013 at 3:36
  • Because this is a premade script, so I find it more convenient to just use it. I am not capable of recoding this in PHP. Commented Oct 3, 2013 at 3:40
  • get rid of <!-- in the begining Commented Oct 3, 2013 at 3:52
  • @deepak, I just tried and still, nothing shows up on my post when I view it. Commented Oct 3, 2013 at 3:54
  • @MichaelDelvege Where are you going to display the date? Is it the date of the post itself? Commented Oct 3, 2013 at 3:55

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Note that since HTML5 the type attrubute is no longer required and defaults to text/javascript.
1

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.

Comments

0

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

1 Comment

This did not work for me, but thanks for helping out man. I have it resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.