/    Sign up×
Community /Pin to ProfileBookmark

How can I use JS/AJAX to fetch the title of the page from the BODY after its loaded?

Hi all, thanks for stopping in on this thread.

So, this is how it would work. There would be some short script in the actual <title></title> tags which would load the actual title, which is in some longer script located in the body of the html page (after the page loaded). I think that AJAX is needed, not sure though. The reason I need this is because I am loading the one and only header(.shtml) of ALL the pages on my website. However, I still need different titles for each. Thanks.?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@VodkaProDrinkerMar 02.2009 — lol, It's simple if you thinking about that, but ajax dosn't need to be in used here. It's a pure old and good JavaScript.

You need to put this where the title's tag are:
[code=html]<title><script type="text/javascript">
var titlee;
function writet(titlee)
{
document.write = titlee;
}
</script></title>[/code]

and then, any time you want to change the title you just write:
[code=html]<script type="text/javascript"> writet(' - here you put the title - '); </script>[/code]
pretty easy isn't it? ?
Copy linkTweet thisAlerts:
@fullyuberedauthorMar 02.2009 — Thanks for your reply. However, I am sorry to say that it doesn't work. Look:

http://fullyubered.com
Copy linkTweet thisAlerts:
@Angry_Black_ManMar 02.2009 — interesting the way this worked..

as i learned from this thread, apparently anything (in this case a script) in a title tag is treated as characters rather than ... whatever else it is. not that the solution VodkaProDrinker offered would have worked in any case, but it is interesting nonetheless.

the solution here is to reference the title of the document and change it accordingly. the method provided above simply would have overwrote the entire HTML document with whatever the variable "titlee" contained.

to reference the title properly, you need to ask the "document" object for its "title" property:

document.title

so after an onload method is run, you'd just set the doument's title to whatever string you want, derived from whatever HTML element youre talking about. but without knowing what on your page contains the title, i cant tell you how to then reference the actual title you want to use.
Copy linkTweet thisAlerts:
@VodkaProDrinkerMar 02.2009 — OMFDG, I can't belive that i skrew up this big XDD

How didn't I thought about document.title? Damm, thanks god aaron.martinas been here to save my sorry ass ?

Any, way my script (lol) wil still work if you will replace document.write at document.title and will locate the script outside the title tags but still inside the headers tags.

And agn, I'm so sorry for my mistake..
Copy linkTweet thisAlerts:
@fullyuberedauthorMar 02.2009 — Sorry for my javascript noobinesss...

But what is the entire code to do this? I am not sure where to put the document.title code. Or anything at all.
Copy linkTweet thisAlerts:
@FangMar 02.2009 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;some title here&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
alert(document.title);
&lt;/script&gt;

&lt;style type="text/css"&gt;
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@fullyuberedauthorMar 02.2009 — I don't think that you guys realize what I'm trying to do. I need a short line of script which you put in the BODY of the webpage, which CONTAINS THE TITLE. Then that title is displayed in the very top bar of the browser, where the normal title is displayed. NOT in an alert or confirm.
Copy linkTweet thisAlerts:
@FangMar 02.2009 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;some title here&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
window.onload=function() {
document.title='The new title';
}
&lt;/script&gt;

&lt;style type="text/css"&gt;
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @fullyubered spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...