PDA

View Full Version : <title></title> in header



dz67
05-17-2007, 10:35 AM
This may sound like a stupid question but I'm sure it has a simple answer.

I have one 'header' asp file that I virtually 'include' in all of my documents. In this header I have a title tag <title>My Website Homepage</title>, for instance. A single file 'footer' is also then virtually 'included' within the documents to close the code out.

How can I change this <title></title> in every page to something different <title>My Website - Page 1</title>, for instance???

Can't think of how to do it and I'm sure there is an easy answer. Does the <title> have to be in the header of ALL documents?

Any help would be greatly appreciated.

Cheers.

codeexploiter
05-17-2007, 01:30 PM
The default location for the title tag is within the <head> tag. Now you have a header.asp file that you virtually include in all your pages, right?

Either you can do something like the following inside your header.asp file



Response.Write "<script type='text/javascript'> window.onload = function() {document.title = 'The Title You Want'; } </script>"

Make sure that you don't have any other window.onload event triggering in any of your pages. If you have then you must handle it appropriately.

mwinter
05-17-2007, 03:51 PM
Response.Write "<script type='text/javascript'> window.onload = function() {document.title = 'The Title You Want'; } </script>"


Ick. I'm not familiar with the inclusion process in ASP, but would it not be possible to define a variable in the document that is including and use that variable in the document to be included?

In PHP, one could write:



<?php
$documentTitle = 'foo';

include('/header.php');
?>
</head>

<body>
<!-- ... -->
</body>
</html>

where header.php contained:



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title><?php echo $documentTitle; ?> ߞ My Site</title>

<!-- ... -->

Is there not something analogous to that with ASP?

alexjewell
06-04-2007, 11:59 PM
No, I've actually tried the above method, mwinter, and for some reason it does not work. This is because the included file is not affected by what it's being included in.

The best, and easiest, way to do this is with the javascript method posted by codeexploiter. Now, you have to understand, though, that this method does not actually change the real title, but just its display. So Google still sees My Website, and not My Website - Page 1. But if the reason is for the viewer to see a more detailed title, then this is the way to go.

Twey
06-05-2007, 12:10 AM
I'd say to have the included file define a function, then, which takes a title as parameter and outputs the header with the correct title. You can then call that from your main page after including the header file.

If this won't work and there's truly no better way to do it than Javascript, get a real server-side language :p

alexjewell
06-05-2007, 12:28 AM
Hahahaha


get a real server-side language

Amen.

protoskore
01-03-2008, 06:41 AM
no one says the virtual include has to be the first thing on the page.
since the header's tag structure is likely to be doctype->html->head->title and then more header code,
you can put just that first bit at the top of each page (with the relevant title) and then continue with the virtual include of the header.

kooshal
01-08-2008, 12:54 PM
Try this:

--------- In each page at the top before include statement -----------
<%
Dim pageTitle
pageTitle = "## Your page title here! ##"
%>
-----------------------------------
--------- In Server Include File ----
<title><%= pageTitle %></title>
-----------------------------------

Upgrade to ASP.NET, it's is easier!

boogyman
01-08-2008, 12:57 PM
Hahahaha



Amen.


no one says the virtual include has to be the first thing on the page.
since the header's tag structure is likely to be doctype->html->head->title and then more header code,
you can put just that first bit at the top of each page (with the relevant title) and then continue with the virtual include of the header.


Try this:

--------- In each page at the top before include statement -----------
<%
Dim pageTitle
pageTitle = "## Your page title here! ##"
%>
-----------------------------------
--------- In Server Include File ----
<title><%= pageTitle %></title>
-----------------------------------

Upgrade to ASP.NET, it's is easier!



this topic is nearly 6months idle. look at the time before you post a reply :)