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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically changing the <Title> of a webpage 1

Status
Not open for further replies.

arundahar

Programmer
Apr 27, 2001
53
GB
Hi, can anyone tell me how to dynamically change the <TITLE> of a webpage. Currently our site displays the same title on ALL pages, but depending on the product that is being viewed i want to change the <Title> tag,

Any ideas?

Thanks

A
 
that's Javascript mate. Not ASP. Anyone know how to do it in ASP (not possible?). Or alternatively, does anyone know how to run that document.title = &quot;new title&quot; via ASP without having an onclick/onsubmit etc...

A
 
How are you writing back to the client?
I assume you are using a frame set and updating a single frame with the product details.

If that is how you are doing it you can send back a block of script that will execute when it is loaded at the client.

Mat
 
no i'm not using any frames!
Take it to the basic level. I have a logon page, user logs in, takes him to another page. Halfway down this second page is ASP that checks whether he has new mail. If he has then i need to change the <TITLE> to NEW MAIL!

So how do i just execute the Javascript passing in the number of emails to change the <TITLE> to &quot;New Mail - You have X mails&quot; where X is the number returned by the ASP.

A
 
If in the body of your page you have something like:

You have <%= NumOfNewMails %> New Mails!

you can add after this:
<%
if NumOfNewMails > 0 then
%>
<script language='javascript'>
document.title=='You have new mail';
</script>
<%End if%>

that way the page you are loading will rename the window.

Hope this helps.


Mat
 
I didn't think that the browser window title could be changed dynamically like that - IMHO the page must be reloaded first. (I know that content IN the browser window can be changed - preferably with JavaScript, VBScript needs a reload - but the window frame is related to the application that runs on the client machine, and here there could be all sorts of incompatibilities between various platforms!)

If I'm right, you need to pass the user on to a new page or force a reload somehow. Then it is relatively simple to change window title. If I'm wrong, I'll be thrilled to learn something new!

I once did something like this: the first page contained some code which defined a title string. I then passed this as a hidden form variable to the second page (when the visitor clicked on a &quot;Continue!&quot; button). This script started with a piece of VBScript which decoded the hidden variable, something like

<% titlestring = request.query(&quot;title&quot;) %>

In the HTML header below, I then set the TITLE attribute with <TITLE><%=titlestring%><\TITLE>

Perhaps this helps?
DrMaggie
---------
&quot;Those who don't know the mistakes of the past won't be able to enjoy it when they make them again in the future.&quot;
— Leonard McCoy, MD (in Diane Duane's Doctor's Orders)
 
I'm guessing another way to do this is with a session variable. It also depends on how your pages are set up. Are your title tags in the page or part of some include file. If in the include it would be so easy to set this up:

Below: This would all be in an include


-- do logic up here --
<% if some condition such as user has successfully logged in then
session.contents(&quot;YOUR_TITLE_VARIABLE&quot;) = &quot;Welcome&quot;
elseif someother condition like today is xmas then
session.contents(&quot;YOUR_TITLE_VARIABLE&quot;) = &quot;Merry Xmas&quot;
end if
%>

<title>
<% = session.contents(&quot;YOUR_TITLE_VARIABLE&quot;) %>
</title>

or just put the logic part in the include file and leave the title tags in the pages.

Hope this helps.

Mike
 
Mike:

Good point - I always tend to forget how useful session variables are :), so thanks for the reminder!

My point is, however, that this type of stuff works when the value of the titlestring is known BEFORE the html code is passed to the client browser - but the original question referred to the wish to dynamically change the title &quot;half-way through the page&quot;, which I took to mean that some HTML content had already been posted to the client.

Now if I misunderstood this, and &quot;halfway through&quot; actually meant after a lot of SCRIPTING had been processed, but the HTML part hadn't yet started, then things change a lot - then there's no need for session variables or passed query data - then the number of e-mails can be figured out already _before_ the code reaches the HTML header and the proper value be inserted without problems!!!

So, arundahar: which one is it?

DrMaggie
---------
&quot;Those who don't know the mistakes of the past won't be able to enjoy it when they make them again in the future.&quot;
— Leonard McCoy, MD (in Diane Duane's Doctor's Orders)
 
When I tested dynamically changing the title I had code attached to a button and it worked fine.

Mat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top