View Full Version : get title from referring URL - javascript instead of PHP

02-15-2007, 01:38 AM
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:
$filesource = $_SERVER['HTTP_REFERER'];
$a = fopen($filesource,"r"); //fopen("html_file.html","r");
$string = fread($a,1024);
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...


02-15-2007, 02:14 AM
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. (http://us3.php.net/manual/en/reserved.variables.php)

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&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=csU&q=javascript+get+file+content+&btnG=Search, http://www.expertsrt.com/tutorials/Rod/JSread.php

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


02-18-2007, 06:30 AM
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?


02-18-2007, 06:51 AM
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.