get title(part) from referring URL - javascript instead of PHP

Discussion in 'JavaScript' started by Felix33, Feb 14, 2007.

Thread Status:
Not open for further replies.
  1. #1
    Hi everybody,
    I need to find a script which would allow me to get the title (in between the HTML title tags) from a referring page. the website has lots of referring url's to it, and the idea is that when a user types in an url,
    the first word of the title will be displayed on the page.

    Now, I came up with a PHP script that works fine, problem is, that I can't use PHP, cause the redirects are all set to the .html pages and I can't change them. the script gets the referring URL, reads the page and extracts the title and breaks it down into words separated by whitespaces.

    Is it possible to achieve the same with javascript?

    here's the php:
    <?php
    $filesource = $_SERVER['HTTP_REFERER'];
    $a = fopen($filesource,"r"); //fopen("html_file.html","r");
    $string = fread($a,1024);
    ?>
    <?php
    if (eregi("<title>(.*)</title>",
    $string, $out)) {
    $outdata = $out[1];
    }
    //echo $outdata;
    $outdatapart = explode( " " , $outdata);

    echo $part[0];

    echo $outdatapart[0];

    ?>

    Any help would be greatly appreciated
    I am not a JS expert, have been searching for a solution
    have seen a couple of similar posts here, but didnt help me with my specific question ...
    Thanks!
    Felix

    p.s.
    I did find a function to find out and write out the referring page, just dunno how to go from here
    head:
    <script>
    function loadRefURL (){
    document.forms[0].theAddr.value = document.referrer;
    </script>
    body:
    <script>
    document.write( document.referrer );
    </script>
     
    Felix33, Feb 14, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Not possible with Javascript.
     
    nico_swd, Feb 14, 2007 IP
  3. Felix33

    Felix33 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Why should it not be possible???
    I believe it is. After all there are JS ways to get referring URLs, readin g and writing files and string functions, I've been finding out that much, just trying to figure out how to put it together...
     
    Felix33, Feb 15, 2007 IP
  4. coolsaint

    coolsaint Banned

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not possible with javascript.
     
    coolsaint, Feb 15, 2007 IP
  5. CBDealer

    CBDealer Guest

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Why couldn't you do it with JS? You'd have to load the page and then you could either grab the content of the title element through the DOM or you could write the page content to a string and regex for the title tags.
     
    CBDealer, Feb 16, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    And how do you load external pages with Javascript? I don't think that's possible.
     
    nico_swd, Feb 16, 2007 IP
  7. CBDealer

    CBDealer Guest

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Load the page into a child window like an invisible iframe or a popunder.
     
    CBDealer, Feb 16, 2007 IP
  8. Felix33

    Felix33 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    found some more info in the meantime, not sure if it will help me
    (sorry I am not allowed to post links yet)

    also I have no access to the referring pages, they are redirected in a frameset redirect to the html page I am working with, which is loaded into a frameset.
    wasn't a problem with the PHP.
    I tried to get to the title with
    document.getElementsByTagName("title")[0].innerHTML = document.title;
    trying to use on the the top.document.title
    but that didn't work, cause the files are on different servers.
    Starting to wonder if this can work...
    to CBDealer,
    I was thinking on s.th. like you are talking about -
    As I am no JS expert - any code hints to how to load the page and write it to a string?
    I am familiar with iframes, have thought about that too,
    Thanks a million
     
    Felix33, Feb 17, 2007 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    You cannot access frames that don't come from your URL. This is not allowed for security reasons. Wouldn't it be easier if you'd just make apache parse your HTML files too?
     
    nico_swd, Feb 18, 2007 IP
  10. Felix33

    Felix33 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hi Nico,
    Just found out that that would be a possibility, would make everything so much easier!
    Our server doesn't give us access to the .htaccess file, but it would be worth changing hosts.
    I am just looking into that,
    Thanks
     
    Felix33, Feb 18, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Yes, plus it would work if the user has Javascript disabled. :)

    Good luck...
     
    nico_swd, Feb 18, 2007 IP
Thread Status:
Not open for further replies.