Get Websites Title

2019-07-17 11:56发布

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

4条回答
等我变得足够好
2楼-- · 2019-07-17 12:28

Unless the URL's href is on the domain of the current document, using JavaScript to try to get the title of the target document would require cross-domain scripting which is not generally allowed (using traditional methods) by browsers. Unless you're real fancy with proxies (not entirely sure how that is done), you'll need a server-side language to load the document first.

查看更多
冷血范
3楼-- · 2019-07-17 12:39

For security reasons, you cannot read content from a different website using Javascript, even just to read the title.

You could write a server-side proxy that requests a remote page and finds its <title> tag using an HTML parser.

However, you shouldn't do this at the client side; it will waste time and resources.
If you really want to do this, do it once on the server as a pre-processing step when you create a new page.

查看更多
The star\"
4楼-- · 2019-07-17 12:41

Yep, just use document.title. Simple and effective.

$('.stack').attr("title", document.title);

EDIT: It looks like I misunderstood your question. If you want to get the title of another page, not the currently loaded page, you could do some cross-domain AJAX trickery, but it's not generally a good idea. I'd just grab the page title server side (in whatever you are using to generate the page [php, asp, etc]) and output it.

查看更多
看我几分像从前
5楼-- · 2019-07-17 12:43

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>
查看更多
登录 后发表回答