Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve window title

Status
Not open for further replies.

darkyoda2

Programmer
Joined
Sep 4, 2002
Messages
8
Location
CA
Hi! Here's my question: how can you retrive the tile of the current window. I have tried the following: document.write(self.document.title); but nothing is inscribed on my page. If I give a title with the following command: document.title = "test"; and then retrieve it, all is well. I see the text "test" on my page. So how can you retrive the title of a window that you have just opened?
 
'yoda,
If the window already has a name assigned, then the following will show it: window.name

If you want the URL of the file viewed within the window use: window.location
 
I'm not sure I see the problem here. This worked fine for me:

<html>
<head>
<title>Test title</title>
<body>
<script>
document.write(document.title)
</script>

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
I trust that you've included a Title in your HTML?

<html>
<head>
<title>My Page Title</title>
</head>
<body>
</body>
</html>

...if so, I assume that the 'document.title' is not valid until the page has LOADED...

So try:

<body onload=&quot;alert(document.title);&quot;>

If that works, but you still need the title to be printed, try setting the innerHTML of a <div> or <span> with the onload event.
 
All of your solutions work but this is not really what I want. The problem is that I have not give a title to my page. I just want to retrieve the default title. When you give a title to a window, the top of the window look like this: &quot;YourTitle - Microsoft Internet Explorer&quot;. If you don't give a title to your window, the top of the Explorer Window looks like this: &quot;adresseOfTheWindow - Microsoft Internet Explorer&quot;. How can I retrieve this default title. This was my question, sorry if I'm not very clear.
 
You could use something like this:
<script>
if(document.title.length==0){
document.write(location.href)
}else{
document.write(document.title)
}
</script>

As for the &quot; - Microsoft Internet Explorer&quot; part, you could use a browser sniffer script to see what kind of browser they're using, then print the name of it out.

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
Yes but I want the title all the time. Thanks for your help by the way.
 
If there's no <title>... then there's no title.

When there's no title, most browsers display the href. IE also always adds &quot; - Microsoft Internet Explorer&quot; to the title/href.

Adam's code is what you're after.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top