Click to See Complete Forum and Search --> : Grab header from page, set it as document.title


dysfunctional
07-31-2003, 10:38 AM
Hi,

Could anyone tell me if it's possible to have javascript grab the header of an article (say, "H1") and then make that the document.title?

I know very little javascript, so I don't know how to set the function document.title=h1... Thanks for your help!

pyro
07-31-2003, 11:49 AM
This will grab the text out of the first <h1> tag and set it to the title of the document:

<script type="text/javascript">
function setTitle() {
document.title = document.getElementsByTagName("h1")[0].firstChild.data;
}
window.onload = setTitle;
</script>document.getElementsByTagName("h1") returns an array of all the <h1> tags, so the first is [0], the second [1], etc...

dysfunctional
07-31-2003, 12:03 PM
I'm impressed! Thank a million, it works perfect.

I spent the last two days trying to find out how to make this happen... So you made my day!

pyro
07-31-2003, 12:09 PM
You bet. I was happy to help... :)