Results 1 to 3 of 3

Thread: Get the iframe's location's page title from a no-locally page?

  1. #1
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get the iframe's location's page title from a no-locally page?

    How to do that?

    I know that I can get a local page title from an iframe by using this code:
    Code:
    iframe.document.title
    But if I want to get an already published page's (like Google) title, how to do that?
    Last edited by bladefinor; 12-23-2008 at 03:56 PM. Reason: It did just work locally

  2. #2
    Join Date
    Dec 2008
    Location
    Nigeria
    Posts
    95
    Thanks
    3
    Thanked 8 Times in 8 Posts

    Default

    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!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    Last edited by jscheuer1; 12-25-2008 at 07:02 AM. Reason: add parenthetical about other server side languages
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •