Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top