How to get the value of the HTML title tag?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    How to get the value of the HTML title tag?

    I was using the below javasciprt code, can the innerHTML be used in PHP? Is it possible to change this code into php?
    Code:
    function get_title() {
    	var t = document.getElementsByTagName('title')[0];
    	if ( !!t.childNodes.length ) {
    		document.write(t.firstChild.data);
    	} else if ( t.innerHTML ) {
    		document.write(t.innerHTML);
    	}
    }
    Last edited by Atli; Jan 24 '10, 08:34 AM. Reason: Thread title clarified.
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    You can output any type of code that will go into the HTML page using PHP.

    Are you trying to get a new page title from the server based on some "on server" processing? If YES, you can make it easier by having a variable defined in JavaScript; I'd suggest an anonymous object or out of GLOBAL scope, and make an AJAX call on a PHP script at the server.

    Generating "in page" JavaScript is done all the time.

    Although after the code generation is done, there is no longer a connection to the PHP script running on the server. YOU can get very sophisticated and replace JavaScript script tag contents on the fly; even via AJAX calls to a PHP script - as mentioned above.

    Code:
    <html>
      <head>
        <title><?php echo $page_title?></title>
        <script type="text/javascript" src="<?php echo getScript()?>"></script>
        <script type="text/javascript">
          <?php
            // Generate a bunch of JavaScript code here...
          ?>
        </script>
      </head>
      <body>
    
      </body>
    </html>

    Comment

    • dgreenhouse
      Recognized Expert Contributor
      • May 2008
      • 250

      #3
      Sorry for the confusing post... I was very sleep deprived...

      A specific line from the original post should be read as:
      "I'd suggest an anonymous object or an out of GLOBAL scope variable"

      Comment

      Working...