获取网站标题 [英] Get Websites Title

查看:259
本文介绍了获取网站标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检索URL的标题的链接。

例如获得此称号:

<一类=堆栈HREF =htt​​p://stackoverflow.com称号=堆栈溢出>

将动态地从这样的事情发生: $('堆')ATTR(称号,......);

有可能与JavaScript或jQuery的,要检索URL的名称?

很多

感谢
解决方案

花了一点时间,使,但这个例子可以让你从你的网页上下载一个网页。然后,从标题标签中提取标题。

 < HTML和GT;
< HEAD>
<! - jQuery的包括 - >
<脚本类型=文/ JavaScript的SRC =htt​​ps://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js>< / SCRIPT><! - 这包括允许跨域get请求 - >
&LT;脚本类型=文/ JavaScript的 src=\"https://raw.github.com/jamespadolsey/jQuery-Plugins/master/cross-domain-ajax/jquery.xdomainajax.js\"></script>&LT;! - 样品 - &GT;
&LT;脚本类型=文/ JavaScript的&GT;
$(文件)。就绪(函数(){
    //获取第一个锚的href
    变种URL = $(一)的第一()ATTR(的href)。;    //设置一个GET请求来获取HTML源代码
    $获得(URL,功能(数据){
        //使用获取函数之间串拿到冠军标记之间的文本
        //然后调用它在消息框中
        警报(getStringBetween(data.responseText,&LT;标题&gt;中,&LT; /标题&gt;中));
    });
});功能getStringBetween(输入,开始,结束){
    VAR指数= input.indexOf(开始);    如果(指数!= -1){
        指数+ = start.length;
        变种endIndex的= input.indexOf(结束,指数+ 1);        如果(endIndex的!= -1)
            返回input.substr(索引,endIndex的 - 指数);
    }
    返回false;
}
&LT; / SCRIPT&GT;&LT; /头&GT;
&LT;身体GT;
    &所述; A HREF =htt​​p://www.google.com/&GT;谷歌&下; / A&GT;
&LT; /身体GT;
&LT; / HTML&GT;

I am trying to retrieve the title of a URL for a link.

For example get the title of this:

<a class="stack" href="http://stackoverflow.com" title="Stack Overflow">

will be generated dynamically from something like this: $('.stack').attr("title", "....");.

Is that possible with javascript or jQuery to retrieve the title of a URL?

Thanks alot

解决方案

Took a little time to make, but this example allows you download a web page from your web page. Then extract the title from the title tags.

<html>
<head>
<!-- jQuery include -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<!-- This include allows cross domain get requests -->
<script type="text/javascript" src="https://raw.github.com/jamespadolsey/jQuery-Plugins/master/cross-domain-ajax/jquery.xdomainajax.js"></script>

<!-- Sample -->
<script type="text/javascript">
$(document).ready(function(){
    //gets the href of the first anchor
    var url = $("a").first().attr("href");

    //sets a get request to get the html source
    $.get(url, function(data){
        //uses get string between function to get the text between the title tags
        //then calls it in a message box
        alert(getStringBetween(data.responseText, "<title>", "</title>"));
    }); 
});

function getStringBetween(input, start, end){
    var index = input.indexOf(start);

    if(index != -1){
        index += start.length;
        var endIndex = input.indexOf(end, index + 1);

        if(endIndex != -1)
            return input.substr(index, endIndex - index);
    }
    return false;
}
</script>

</head>
<body>
    <a href="http://www.google.com/">Google</a>
</body>
</html>

这篇关于获取网站标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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