0

I have below JavaScript Code. It not getting called when page is loaded. Even Alert is not displaying; moment.min.js is placed in same folder..

<script type='text/javascript' src="moment.min.js">
alert("Testing Hello...")
var mysql_date = '2013-01-25 10:00:00';
var date = moment(mysql_date , 'YYYY-MM-DD HH:mm:ss'); // new moment.js object.

// To display the date in a different format use:
var date_format1 = date.format('MMM, Do'); // Format here would be Jan, 25th
var date_format2 = date.format('MMMM, Do, YYYY'); // January, 25th, 2013
alert(date_format1);
alert(date_format2);
console.log(date_format1, date_format2);
</script>

Please let me know why this script is not loaded when page is loaded

2
  • There is probably something else on your page throwing a JavaScript error. There are no errors in your console? Also, it's a good idea to always use semicolons, even if they aren't technically needed. It's also generally a good idea to not use a src on a script tag, as well as including code within. It works, but it is debatable whether or not it will continue to work in the future. Commented Jan 26, 2013 at 8:36
  • No it does not work. Whatever is inside the script tag with a src is ignored, hence Logan's problem Commented Jan 26, 2013 at 8:42

3 Answers 3

4

either content or src

try this

<script type='text/javascript' src="moment.min.js">
</script>

<script>
alert("Testing Hello...")
var mysql_date = '2013-01-25 10:00:00';
var date = moment(mysql_date , 'YYYY-MM-DD HH:mm:ss'); // new moment.js object.

// To display the date in a different format use:
var date_format1 = date.format('MMM, Do'); // Format here would be Jan, 25th
var date_format2 = date.format('MMMM, Do, YYYY'); // January, 25th, 2013
alert(date_format1);
alert(date_format2);
console.log(date_format1, date_format2);
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

missing ; in first alert i.e alert("Testing Hello...");
3

For the calling the JavaScript on page and include the JavaScript file below is solution

<script type='text/javascript' src="moment.min.js"> </script>
<script type='text/javascript'>
window.onload=function() {
  alert("Testing Hello...")
  var mysql_date = '2013-01-25 10:00:00';
  var date = moment(mysql_date , 'YYYY-MM-DD HH:mm:ss'); // new moment.js object.

  // To display the date in a different format use:
  var date_format1 = date.format('MMM, Do'); // Format here would be Jan, 25th
  var date_format2 = date.format('MMMM, Do, YYYY'); // January, 25th, 2013
  alert(date_format1);
  alert(date_format2);
  console.log(date_format1, date_format2);
};
</script>

Comments

2

Why include src attribute when your are including javascript directly?

Add the code in moment.min.js and just write <script src="moment.min.js"></script>

OR

<script src='moment.min.js'></script>
<script>
alert("Testing Hello...")
var mysql_date = '2013-01-25 10:00:00';
var date = moment(mysql_date , 'YYYY-MM-DD HH:mm:ss'); // new moment.js object.

// To display the date in a different format use:
var date_format1 = date.format('MMM, Do'); // Format here would be Jan, 25th
var date_format2 = date.format('MMMM, Do, YYYY'); // January, 25th, 2013
alert(date_format1);
alert(date_format2);
console.log(date_format1, date_format2);
</script>

Comments

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.