Forum Moderators: open

Message Too Old, No Replies

using document.write in <title> tag not working

using document.write in <title> tag not working

         

sethp

5:03 pm on Jul 1, 2004 (gmt 0)

10+ Year Member



I have some string variables in a strings.js file, like this:

var overview = "Overview of title of page";

However, when I use this in my HTML, all the strings are loaded, except for the string that I want to use for my <TITLE>. This is the code that I use in my HTML to call the external js file, then try the document.write for the overview string:

...
<SCRIPT type="text/javascript" language="javascript" SRC="strings.js"></SCRIPT>
<TITLE><script language="javascript">document.write(overview)</script></TITLE>
</HEAD>
...

The title on the browser then prints out everything between <TITLE> and </TITLE>.

Is this a limitation for the title tage, or is the JavaScript file not loading quickly enough? All the document.write's in the body are working just fine.

Bernard Marx

5:24 pm on Jul 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this a limitation for the title tage, or is the JavaScript file not loading quickly enough?

I don't know the answer to that. You could try:

<script>document.write("<title>"+overview+"<\/title>")</script>

Although it's probably simpler to:

<title>default text</title>
<script>
document.title = overview
</script>

With the 2nd way, you have the opp to put something slightly useful in as a default, for SE, JS-disabled etc

sethp

5:54 pm on Jul 1, 2004 (gmt 0)

10+ Year Member



This did not work:

<script>document.write("<title>"+overview+"<\/title>")</script>

This worked nicely, and leaves a default value, as you mentioned:


<title>default text</title>
<script>
document.title = overview
</script>

Thanks for the tip!

Still wondering why the JS doesn't load in time for the <TITLE> tag.

Bernard Marx

8:04 pm on Jul 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure either, since it works the other way.
To be 100% sure, put the statement in the linked script itself, I suppose.