Javascript string construction by concatenation?

834 views Asked by At

Exploring Javascript (and coming from the Java world). I have the following line of code in a script:

if (jQuery) {  
    document.getElementById("BOTTOM_MID").innerHTML
        = "JQuery Loaded - " + getTime();
}

but it does not work. BOTTOM_MID is not initialized. Yet, the following works:

if (jQuery) {  
    document.getElementById("BOTTOM_MID").innerHTML
        = "JQuery Loaded";
}

Doesn't Javascript understand string construction by concatenation? If yes, how should I proceed?

2

There are 2 answers

7
James Hill On BEST ANSWER

getTime() is a method of the Date object. Try this:

if (jQuery) {  
    document.getElementById("BOTTOM_MID").innerHTML
        = "JQuery Loaded - " +  new Date().getTime();
}

Since you're using jQuery, I also suggest that you use jQuery's ready() handler and selectors:

$(document).ready(function() {
    $("#BOTTOM_MID").html("JQuery Loaded - " +  new Date().getTime());
});

Here's a working fiddle.

0
Pablo Fernandez On

Your code looks fine, try to run this on your chrome developer tools or firebug's console, in this web page:

 document.getElementById("notify-container").innerHTML = "JQuery Loaded - " + "hello"

It will work, indeed.

Perhaps you don't have getTime() defined? It's not a standard javascript function BTW