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

how to get <title>?

Status
Not open for further replies.

2819

Technical User
May 6, 2008
10
NO
I want to add css to a current page in the main menu div by javascript.

Since I am using a old CMS, I am not do it by adding a code to html.

How can I do it?

What I am thinking is that if I use the title in head section the same as the menu, there must be way to do it?

Is it?

Code will be like this and I want to add class="current" in the current page.

<head>
<title>about us</title>
</head>

<body>
<div id="menu">
<ul>
<li>home</li>
<li>about us</li>
......
</ul>
</div>

Thanks in advance.
 
I think it's best just to say that you don't know what you are suggesting. I would suggest you include a linked CSS file in the main layout for the site, and then attempt to style using the C(ascade) part of CSS. Nothing to do with Javascript.

Buy a book on CSS or something.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
I can add class to an element or tagname.

<script type="text/javascript">document.getElementById("menu").getElementsByTagName("li")[0].className = "first";
document.getElementById("sidebar").getElementsByTagName("ul")[1].getElementsByTagName("li")[0].className = "first";

</script>

Is there any way to add class to a current page by finding the current page from <title> in the head section.

 
You can get the page title using Javascript.

I Googled for it and it took about 10 seconds, really.


You can set the CSS class of the body using Javascript.

I Googled again, 15 seconds this time.


I wrote a quick test script which appears to work ok in Firefox at least. Another minute or so wasted. Well it had 1 line and an window.onload handler.

Code:
document.body.className = document.title

Easy, see. Though it may not work in IE etc. I'm not going to mess around testing it.

So yes, you can do what you are asking. But as I think Jeff made apparent. Why would you want to/need to? It's the wrong way to do it.

Oh, you can't have spaces in CSS class names either so your page titles will look wrong and what you are suggesting is a huge misuse of one of the most important HTML tags for page optimisation.

So basically, although you can do it. You shouldn't.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Thanks Farmcow,

Yes, I know that I shouldn't do it.

But I have no choice. The CMS my customer is using is very outdated. It is running on NET. There are many other weird things happening with this CMS.

I personally use Joomla and I love it. When I compare Joomla and this outdated CMS is like New model sports car and Old honkey donkey.

So if I get the title, could you tell me how to place css to the same name in the menu?

Thank you for your help.
 
You want to apply a class to an HTML element that is wrapping a menu?

I dunno. Depends on the markup used. For the first part of the question I used Google. Have you tried that. The address for Google is
Have a go yourself, then if you're still stuck, come back here and show us your efforts and some code.

I'd be surprised if there isn't a way to do what you want within the CMS or by editing the source code. Sounds like you're approaching the problem the wrong way with the assumption you can't do it.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
No probs SQL*enis ;-)

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
[cow]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

Finally, <. is a good thing!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top