0

I am trying to add javascript to a php file and everytime I add the javascript and save, it just displays the javascript and not the rest of the page content.

The code looks like this:

<section class="portlet grid_6 leading"> 

                    <header>
                        <h2>Time <script type="text/javascript">
<!--var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
minutes = "0" + minutes
}
document.write(hours + ":" + minutes + " ")
if(hours > 11){
document.write("PM")
} else {
document.write("AM")
}
//-->
</script></h2>

Any suggestions?

3
  • 1
    can you show us the rest of the page too please? Commented May 6, 2012 at 13:00
  • JavaScript uses // for single line comment and /* ... */ for multi-line comment. Commented May 6, 2012 at 13:08
  • @Coder looks more like a misguided attempt at using CDATA than an attempt to comment out a piece. Commented May 6, 2012 at 13:11

3 Answers 3

2

I suppose you want to display time in that , modify your code like this.

<section class="portlet grid_6 leading"> 
<header>
    <h2>Time<span id="time_span"></span></h2>
</header>
<script type="text/javascript">
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    if (minutes < 10){
        minutes = "0" + minutes
    }
    var t_str = hours + ":" + minutes + " ";
    if(hours > 11){
        t_str += "PM";
    } else {
       t_str += "AM";
    }
    document.getElementById('time_span').innerHTML = t_str;
</script>
</section>

And please next time put more effort in writing the question... formatting etc makes it easier to understand your code and question

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Sarvesh Kumar Singh. Anyway to make this update automatically?
0

Why not using PHP? <h2>Time <?php echo date("g:i A"); ?></h2> (make sure you understand the difference between server time and client time)

If your JS is shown, your code is probably misinterpreted (at least not as text/html). Try removing the HTML comment sets (<!-- & -->).

1 Comment

I could use <h2>Time <?php echo date("g:i A"); ?></h2> however I need the time to automatically update, which why I was trying javascript. Anyway to easily make the time update using this way. I don't want to overload the server either with request to much for new times.
0

How is this being included in your page? If you are reading it in via php (as an include or an ajax request) and using an XHTML doctype, you may need to pre-pend your Javascript with the CDATA tag (see here) so that it is interpreted correctly.

I also +1'd @Sarvesh Kumar Singh's response as separating your function from its display makes for more readable and debugable code.

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.