Forum Moderators: open

Message Too Old, No Replies

Variable to Title Tag Question

         

Jon_King

7:05 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is is possible to pass this string:

window.location.search.substring(1)

into the Title tag of the same .html page?

In other words I'm trying to write that substring as the page title in a static page.

alanderson

8:45 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



you can try to with this;

<title><script>document.write(window.location.search.substring(1))</script></title>

I have some doubts since I am not certain whether the browser will evaluate the title before or after the javascript command is evaluated.

It would be certain to work if it was done server side with php,perl,asp,jsp,etc...

texmex

12:28 am on Jun 22, 2006 (gmt 0)

10+ Year Member



This would work:

<script type="text/javascript">
window.onload=function()
{
document.title=window.location.search.substring(1);
}
</script>

Jon_King

7:05 pm on Jun 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks texmex. Works fine with one exception.

The whole reason I wanted to do this was to present a correct page title to SE's. I however find with this method (using sim spider from the tools here), that the file name of the html file is seen as the Title and not the dynamic Title generated.

It displays perfectly in the browser but the sim spider sees only the file name, in this case search.html

I think, (and I'm pretty stupid on this subject) what I'm looking for is not possible this way?

Bernard Marx

8:28 pm on Jun 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No. Not with client-side script.

Any server-side script will do it easily (ASP,PHP,..)

Jon_King

8:54 pm on Jun 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Bernard. I thought so. If an example is easy can you post some code for that.

Bernard Marx

10:24 pm on Jun 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's an ASP/JScript example.

------------

<%@ LANGUAGE = JScript %>
<html><head><title><%=[CODE_HERE]%></title>

-----------

Where

[CODE_HERE]
is:

1)

Request.QueryString

This is equivalent to the client-side,

location.search.substring(1)
.
Encoded characters, like %20, are not resolved.

or, 2)

Request.QueryString("title")

Gets the value for the title attribute (eg.

?title=bananas => bananas
)

If there is no title attribute in the query string, the output is an empty string.
If there are more than one, their values will appear separated by commas (but let's assume we won't be getting more than one in any query string).