Log in

View Full Version : Get the iframe's location's page title from a no-locally page?



bladefinor
12-23-2008, 03:25 PM
How to do that?

I know that I can get a local page title from an iframe by using this code:


iframe.document.title


But if I want to get an already published page's (like Google) title, how to do that?

diltony
12-24-2008, 09:16 PM
The problem here is that if u make use of pure ajax without any supporting server script, it can only work within your domain, if u wish to get info on pages outside your domain, then use php or something. I am good with ajax and php, but not too efficient with asp, so lemme give u a php and an ajax (javascript code) to do that:

getPageTitle.html

<title>Get Page Title</title>

<script>
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}


function getTitle(url) {
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
if (mygetrequest.status==200){
var data=mygetrequest.responseText;
// regexp which parses text for value between <title> .. </title> tags
var title2 = new RegExp("<title>[\n\r\s]*(.*)[\n\r\s]*</title>", "gmi");
var title = title2.exec(data);
alert(title);
}
else{
alert("The url '"+url+"' is not accesible.");
}
}
}

var request=url+"?cache="+new Date().getTime();

mygetrequest.open("POST", request, true)
mygetrequest.send(null)

return;
}

//getTitle("http://localhost/");
getTitle("http://www.mwebng.net/");
</script>




getPageTitle.php
<?php
$url="http://www.mwebng.net/";
function getTag($string, $tagname) {$pattern = "/<$tagname>(.*?)<\/$tagname>/";preg_match($pattern, $string, $matches);return $matches[1];}
echo(getTag(file_get_contents("$url"),"title"));
?>

See, it is easier done with php!

jscheuer1
12-25-2008, 06:51 AM
Illegal in javascript. In PHP (and other server side languages, if available), this (the availability of importing data from another domain) is controlled by your host's settings.