Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys could you help me please I want to somehow set my html title to display the folder name. I mean if my index.html is in mydomain.com/how it works/ I want the title of index.html to show how it works as a title I know it is possible with php but I am not using php only html help me please.
Thanks
Posted

Assuming your html file (index.html) _doesn't_ have a title element already -
If so, just locate it and replace it's innerHTML

EDIT: Code snippet suplimented with fully-functional example. Also, method of extracting folder name changed. A url of http://localhost/enhzflep/audio/index.html was giving the result of enhzflep/audio

** Obselete **
JavaScript
function myInit()
{
	// When I open the file, it is found at:
	// http://www.localhost/enhzflep/index.html
	//
	// Yours would be http://someDomain.com/PartYouWant/index.html

	// sets locationStr = "/enhzflep/index.html"
	var locationStr = window.location.pathname;

	// make sure the below line reflects the actual name of the file
	// sets locationStr = "enhzflep"
	locationStr = locationStr.replace("/index.html", "");
	locationStr = locationStr.replace("/", "");
	
	// ** NOTE **
	// I make no provisions for folders that have spaces in their names (%20 in the url)
	// you'll need to unencode these yourself. There's likely a function for it urlencode
	// or urlescape or something similar. Never needed it, haven't remembered it yet.
	var title = document.createElement('title');
	var titleText = document.createTextNode( 'Page Title = ' + locationStr );
	title.appendChild( titleText );
	document.head.appendChild(title);	
}



Example of use:
XML
<!doctype html>
<html>
<head>
<script>
window.addEventListener('load', myInit, false);

function myInit()
{
    if (titleElementExists() == true)
        document.getElementsByTagName('title')[0].innerText = constructPageTitleStr();
    else
        addPageTitle();
    /*
        other initialization code here.
    */
}

function titleElementExists()
{
    var titleEl = document.getElementsByTagName('title');
    if (titleEl.length == 0)
        return false;
    return true;
}


function constructPageTitleStr()
{
    var locationStr = window.location.pathname;

    // make sure the below line reflects the actual name of the file
    // sets locationStr = "enhzflep"
    locationStr = locationStr.replace("/index.html", "");

    // get the position of the last / char in the string
    var slashPos = locationStr.lastIndexOf("/");

    // extract the string that remains after it
    locationStr = locationStr.substr(slashPos+1);
    return locationStr;
}

function addPageTitle()
{
    var locationStr = constructPageTitleStr();
    var title = document.createElement('title');
    var titleText = document.createTextNode( 'Page Title = ' + locationStr );
    title.appendChild( titleText );
    document.head.appendChild(title);
}
</script>
<title>Original Page Title</title>
</head>
<body>
</body>
</html

>
 
Share this answer
 
v4
Comments
Member 9739402 12-Apr-13 8:44am    
thanks very much could you tell me please how to implement this code in to html
enhzflep 12-Apr-13 9:05am    
You're most welcome. Sure thing, I've updated the solution to include a full example.
Member 9739402 12-Apr-13 9:06am    
thank you very very much you are my savior
enhzflep 12-Apr-13 9:07am    
:-)
You're welcome.
Member 9739402 12-Apr-13 9:17am    
Hi thanks for your help just one question is there code for instance like leaving <title></title> empty and and the code will add the title according to the directory index.html in thanks very much
HTML
<head>
   <title>how it works</title>
</head>
 
Share this answer
 
Comments
DinoRondelly 11-Apr-13 13:43pm    
+5
Richard C Bishop 11-Apr-13 14:13pm    
Classic.
enhzflep 11-Apr-13 14:49pm    
<-- Insert smutty fellatio joke here -->

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900