How to get current html page title with javascript

旧时模样 提交于 2019-12-17 11:03:49

问题


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

I use firefox and with

document.title 

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.

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


回答1:


One option from DOM directly:

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

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

Or more generic

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



回答2:


try like this

$('title').text();



回答3:


Like this :

jQuery(document).ready(function () {
    var title = jQuery(this).attr('title');
});

works for IE, Firefox and Chrome.




回答4:


$('title').text();

returns all the title

but if you just want the page title then use

document.title


来源:https://stackoverflow.com/questions/9228947/how-to-get-current-html-page-title-with-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!