javascripthtmlcsswebgoogle-web-designer

document.title problems in Javascript


I am new to JS and learning from a JAVASCRIPT and JQUERY book by JON DUCKETT.

I was struck at a problem regarding the document.title usage

I wrote a code on how to use document objects.

Every property and methods are working except for the title

I attached a code please have a look and help me how to get the title property working.

The code is as following :

<!DOCTYPE html>
<html>
<head>
  <title>Hammer</title>
</head>
<body>
  <h1>THANOS</h1>
  <div id="tony"></div>
</body>
<script>
    var lord = "<p> The title of the page is :" + document.title + "</p>";             
     
     lord += "<p>address of the page is : " + document.URL + "</p>";

     lord += " <p>Last modified is : " + document.lastModified + "</p>";

     var man = document.getElementById("tony");
     man.innerHTML = lord;
</script>
</html>

output: THANOS

 The title of the page is : 

 address of the page is : https://fiddle.jshell.net/_display/

 Last modified is : 07/14/2018 10:28:57

title not displaying why?

And what is the meaning of the += in the code please help

Sorry I don't have the reputation of 10 to post an image I had to write this code.


Solution

  • Your code is actually working. See it live by hitting the Run the code button below:

    var lord = "<p> The title of the page is :" + document.title + "</p>";             
    
     lord += "<p>address of the page is : " + document.URL + "</p>";
    
     lord += " <p>Last modified is : " + document.lastModified + "</p>";
    
     var man = document.getElementById("tony");
    
     man.innerHTML = lord;
    <!DOCTYPE html>
    <html>
    <head>
    <title>Hammer</title>
    </head>
     <body>
     <h1>THANOS</h1>
     <div id="tony"></div>
    </body>
    </html>

    And, when we write something like a += b in any programming language (not just JavaScript), it is simply a shorthand to write a = a + b. Nothing more, nothing Less. :)