Hi guys

I hope this is the best section to get my answer.

I have 2 web pages that use the same html header markup via a template web page.

Know I use this template webpage to define my head in my webpage but I wish to redifine it so it can be page specific instead of being a general template.

Is this possible?

Coded Example:

<html>
 <head>
  <title></title>
  <meta ... />
  etc
 </head>
 <body>
   <!-- 
    can i redefine the header in this section??? something like 
   -->
   <head> <title><title> <meta /> etc </head>
 </body>
</html>

Now is something like this possible?
In short I wish to redefine the head children, how would I do this?

Thankyou, Regards X

Dani AI

Generated

— putting a second <head> inside the <body> is invalid HTML and will behave unpredictably. was right that you need scripting or server-side logic to vary head content; is correct that the example given won’t work as-is. Two practical, safe approaches follow.

Server-side templating (recommended): emit the page-specific head on the server so each page serves the right <title>, meta tags and social tags to browsers and crawlers. In PHP you typically set variables then include a single header template:

<?php
$pageTitle = 'Contact';
$pageDescription = 'Short description for this page';
include 'header.php';
?>

header.php prints those values into <head> using htmlspecialchars() so output is safe.

Client-side mutation (works for users, not always for crawlers): JavaScript can change document.title and add/update <meta> tags at runtime without rewriting the page. Example:

<script>
document.title = 'Page-specific title';
let m = document.querySelector('meta[name="description"]');
if (m) m.content = 'New description';
else {
  m = document.createElement('meta');
  m.name = 'description';
  m.content = 'New description';
  document.head.appendChild(m);
}
</script>

When to pick which: use server-side rendering or build-time templates if SEO, social previews (Open Graph), or initial-page performance matter. Use client-side updates only for SPAs or purely UI changes where search engines and bots are not a concern. Test by viewing source and by using curl or the platform’s crawler preview — if a tag is added only with JS it may not be seen by some scrapers.

Recommended Answers

All 5 Replies

As far as I know, what you have done there is invalid. If you want to do something like this, you will need to use some scripting to change the head depending on your conditions. The best way would probably be to use a server-side language.

Ya I thought I might ask in HTML first but I was wondering.

Ya it is invalid code but I was hoping for sort of solution that is similar to the example where the head can be changed in the body if anyone has one :(

I don't understand exactly what you are trying to do.

What you presented is an example of the typical form of a web page, not code that works.

Its an example, in short I want to redefine the title, meta etc in the body tag.

Is this possible? (I gather have to use php)

I think you would be able to use JavaScript in the body section, but that would essentially just re-write you whole web page. When the html is read, the head is read before the body, so I don't think you can re-define the head in the way you want to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.