Get Websites Title
Solution 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 --><scripttype="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><!-- This include allows cross domain get requests --><scripttype="text/javascript"src="https://raw.github.com/jamespadolsey/jQuery-Plugins/master/cross-domain-ajax/jquery.xdomainajax.js"></script><!-- Sample --><scripttype="text/javascript">
$(document).ready(function(){
//gets the href of the first anchorvar 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 boxalert(getStringBetween(data.responseText, "<title>", "</title>"));
});
});
functiongetStringBetween(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);
}
returnfalse;
}
</script></head><body><ahref="http://www.google.com/">Google</a></body></html>
Solution 2:
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.
Solution 3:
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.
Solution 4:
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.
Post a Comment for "Get Websites Title"