2

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

jQuerybeast
  • 14,130
  • 38
  • 118
  • 196

4 Answers4

1

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>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Drake
  • 3,851
  • 8
  • 39
  • 48
  • This will only work within your domain. Also, you should parse the HTML using jQuery instead of doing string matching. (What about ``?) – SLaks Nov 09 '11 at 02:06
  • @SLaks how about you attempt the code I just gave before claiming it doesn't work. – Drake Nov 09 '11 at 05:00
  • You are using a server-side proxy; I hadn't noticed that. – SLaks Nov 09 '11 at 13:50
0

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.

jli
  • 6,523
  • 2
  • 29
  • 37
0

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Right okay now I get this. Basically I am looking for something like the famous bookmark sites use. For example http://delicious.com/ gives you the title of that page... Does that makes sense? – jQuerybeast Nov 09 '11 at 01:49
  • They do it in advance on the server. – SLaks Nov 09 '11 at 01:50
  • On your edit: It's a little bookmark site. I can't think of another way to do this. I mean the client adds a website, you need to give him a title... – jQuerybeast Nov 09 '11 at 01:50
  • You mean using server-side proxy? – jQuerybeast Nov 09 '11 at 01:52
  • Yes. If you need to show the title as the user adds before reloading, you'll need to use AJAX with a proxy. Otherwise, you should look up the title when you add a bookmark and store it in the DB. – SLaks Nov 09 '11 at 02:07
0

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.

Joel Salisbury
  • 175
  • 1
  • 6