Jump to content

Using the value of <title> element


RRRSystems

Recommended Posts

Hello,Is it possible to use the value of the <title> in other places inthe document with out replacing.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>GWT Application</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link id="application-css" type="text/css" rel="stylesheet" href="Application.css"> <script type="application/javascript" src="application/application.nocache.js"> </script> </head> <body> <!-- How can I get the value of <title> to be used for the <h1> --> <!-- or any other place else that I would like to use it? --> <h1>I would like this to end up being:GWT Application</h1> <!-- I would like this to happen automagically, not replace. --> </body></html>Thanks,Raney

Link to comment
Share on other sites

you could probably use Javascript. After the DOM has loaded you could call a javascript function to find the value for an elements title attribute and then assign it to whatever elements need it.

Link to comment
Share on other sites

JavaScript would work but has some draw backs. 1. there can be a delay in setting the value depending on the size of the document and how the code is written.2. If JavaScript is turned off or an addin like NoScript is being used the H1 tag will remain empty.3. If you are using the H1 tag for SEO purposes then JavaScript will notwork as search engines do not execute JavaScript and to them the H1 tag would be empty.Your best bet is to use a server side solution like PHP.

<?php   $title = "GWT Application"; ?><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title><?php echo $title; ?></title><meta http-equiv="content-type"content="text/html;charset=UTF-8"><link id="application-css"type="text/css"rel="stylesheet"href="Application.css"><script type="application/javascript"src="application/application.nocache.js"></script></head><body><!-- How can I get the value of <title> to be used for the <h1> --><!-- or any other place else that I would like to use it? --><h1><?php echo $title; ?></h1><!-- I would like this to happen automagically, not replace. --></body></html>

Link to comment
Share on other sites

If you wish to disregard aspnetguy's advice, JavaScript can help you out. Since there is only ever one <title> element, it is customary to use the following syntax to get the contents:document.getElementsByTagName('title')[0].innerHTMLIt looks a little funky at first, but it's common, and once you get used to the pattern, you'll find lots of applications for it.

Link to comment
Share on other sites

If you wish to disregard aspnetguy's advice, JavaScript can help you out. Since there is only ever one <title> element, it is customary to use the following syntax to get the contents:document.getElementsByTagName('title')[0].innerHTMLIt looks a little funky at first, but it's common, and once you get used to the pattern, you'll find lots of applications for it.
document.title :)
Link to comment
Share on other sites

Always seems like cheating. :)
document.title is actually the only way to modify the window title after the page has loaded. I've tried.I don't mind using document.body and document.title as they are proper DOM objects.
Link to comment
Share on other sites

Thank you,I have checked out all of the suggestions.The documentation for all ofthese options on this site is great, just need to be pointed in the right direction.For what I need to do now I think this approach is the best.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <title>Test</title> <link href="Test.css" rel="stylesheet" type="text/css" media="all" /> </head> <body id=test-body> <h1><script type="text/javascript"> document.write(document.title); </script> </h1> </body></html>Thanks,Raney

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...