Get website titles

I am trying to get the title of the url for a link.

For example, get the name of this:

<a class="stack" href="" title="Stack Overflow">

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


Is it possible for javascript or jQuery to fetch the url header?

Thank you so much


source to share

4 answers

It took a little time, but this example allows you to load a web page from your web page. Then extract the title from the title tags.

<!-- jQuery include -->
<script type="text/javascript" src=""></script>

<!-- This include allows cross domain get requests -->
<script type="text/javascript" src=""></script>

<!-- Sample -->
<script type="text/javascript">
    //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;

    <a href="">Google</a>




Yes, just use document.title

. Simple and effective.

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


EDIT: Looks like I misunderstood your question. If you want to get the title of another page, not the page being loaded, you can do some cross-domain AJAX trickery, but that's not a good idea. I would just grab the server side of the page header (in whatever you use to create the page [php, asp, etc.]) and output it.



For security reasons, you cannot read content from another site using Javascript, even to read the title.

You can write a server side proxy that requests a remote page and finds its tag <title>

using an HTML parser.

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



If the href url is not specified in the domain of the current document, using JavaScript to get the title of the target document will require cross-domain scripting, which is usually not allowed (using traditional methods) by browsers. Unless you're really interested in proxies (not entirely sure how to do this), you'll first need a server-side language to load the document.



All Articles