如何使用javascript获取当前的html页面标题 [英] How to get current html page title with javascript

查看:133
本文介绍了如何使用javascript获取当前的html页面标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript获取普通的html页面标题。

I'm trying to get the plain html page title with javascript.

我使用firefox和

I use firefox and with

document.title 

我得到额外的 - Mozilla Firefox 到标题的末尾。我知道通过修改字符串很容易摆脱这个,但是如果他们改变文本,使用不同的格式等,或者其他一些浏览器修改它,我再次有额外的文本。

I get extra "- Mozilla Firefox" to the end of the title. I know it would be easy to get rid of this by modifying string but if they change text, use different format etc or some other browser modifies this differently I have extra text there again.

那么,有没有任何跨浏览器的方式来获取javascript的普通标签内容? Jquery解决方案没问题。

So, is there any cross browser way to get the plain tag content with javascript? Jquery solution is ok.

推荐答案

直接来自DOM的一个选项:

One option from DOM directly:

$(document).find("title").text();

仅在chrome& IE9,但逻辑上应该适用于所有浏览器。

Tested only on chrome & IE9, but logically should work on all browsers.

或更通用

var title = document.getElementsByTagName("title")[0].innerHTML;

这篇关于如何使用javascript获取当前的html页面标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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