Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question get title from referring URL - javascript instead of PHP

    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 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];

    ?>

    Thanks for your help, I am not a JS expert, have been searching the web for a while but didn't find anything helpful...

    desperate,
    desertrat

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,830
    Thanks
    21
    Thanked 157 Times in 148 Posts
    Before you embark on this path, you may want to ask yourself if what you're doing and how you're doing it is best practices coding and also how important is it that your code rely on an accurate referring URL...

    First of all, in PHP, $_SERVER['HTTP_REFERER'] is crap and shouldn;t be relied upon:

    'HTTP_REFERER' -- The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.


    In PHP, you should be transferring the referring URL via SESSIONS or maybe a database.

    In JS, there is the inherent problem you get with all client side coding...it's client side, so the code can be tampered with and modified...Thus, retrieving the referring URL using client side code can;t really be relied upon either...

    If you must use JS to do this, I would use cookies...create a cookie on the client's computer that contains the referring URL, and on the subsequent page, access the cookie info, open the referring page with JS (referring page will have to be a local file to your server), and then extract the title tag contents using either the DOM or some regex (DOM is probably easier)...

    About JS cookies: http://www.w3schools.com/js/js_cookies.asp

    About manipulating files with JS: http://www.google.com/search?hl=en&s...t+&btnG=Search, http://www.expertsrt.com/tutorials/Rod/JSread.php

    And, using the DOM, access the referring page's title tag with:

    Code:
    document.getElementsByTagName("title")[0].innerHTML;
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi R.J:
    Thanks for your answer and all the info.
    I share your concerns about safety.

    The problem with this project is, that I cannot change anything on the referring pages and also theses pages are redirected to the landing pages via Frameset redirect, so that the title/URL is seen in the browser and the page I am working with loaded into the frameset.
    So I won't be able to use cookies.

    So is there anyway I can access the referring page title?
    got a function to get the referring URL
    function loadRefURL (){
    document.forms[0].theAddr.value = document.referrer;
    }
    and a function with your code to get the title, but couldn't get them together.

    Tried the top.document.title but I guess it won't work, cause the 2 files in the frameset are different servers.

    Now I am really stuck and wonder if this can work at all, given the conditions?

    desertrat

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    1. Javascript will never work across different domains - that would cause all sorts of security issues so it is permanently disabled in all decent browsers.

    2. A lot of firewalls automatically blank out the referrer field in the header to protect the computer owner's privacy by not letting you know what page they came from. Some people configure their browser to blank out the referrer field if their firewall doesn't offer that option.

    The only way that will always work in order to do what you want is to have the referring page set up a session to pass the required information (as some people disallow cookies as well). If you can't change the referring pages then you will never get it to work properly.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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