M-A-X
M-A-X

Reputation: 492

How to set title after html

For example you have output <head> tag and don't know title at that moment.

You will know title at the end of code.

How to set title from that place?

Simplified template looks like:

<html>
    <head>
        <title>We don't know title</title>
    </head>
    <body>
        <article>
            <!--at this place we include code, that output article and know title-->
        </article>
    </body>
</html>

Thanks.

Upvotes: 0

Views: 100

Answers (1)

Tobin
Tobin

Reputation: 2018

First thing to try, the code that is calculating the title I assume occurs after you have outputted the <head> HTML. Are you able to move this code to before the <head> and store any HTML in a variable to be printed after?

Otherwise you would need to set the answer using a script that executes when the page is fully loaded. Something like

//index.php
<head>
 <title></title> //blank title
</head>
<body>
 <?php
  //code that finds title
   $title = 'I am set later';
  ?>

//All other page output

 <script>
  //This will run at the end of the body after everything else.
  document.title = <?php echo $title; ?>

 </script>
</body>
</html>

edit: nicely caught

Upvotes: 2

Related Questions