html标题显示为文件夹名称 [英] html title to show as a folder name

查看:223
本文介绍了html标题显示为文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,你能帮我吗,我想以某种方式设置我的html标题来显示文件夹名称。我的意思是,如果我的index.html在mydomain.com/how它工作/我希望index.html的标题显示它如何工作作为标题我知道它是可能的PHP但我不是使用PHP只有HTML帮助我请。

谢谢

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

推荐答案

假设你的html文件(index.html)_doesn''t_已经有了title元素 -

如果是这样,只需找到它并替换它的innerHTML



编辑:代码片段以全功能示例为例。此外,提取文件夹名称的方法已更改。 http://localhost/enhzflep/audio/index.html的网址给出了enhzflep / audio的结果



** Obselete **

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

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 **
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:

<!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



>


>


<head>
   <title>how it works</title>
</head>


这篇关于html标题显示为文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆