Forum Moderators: mack
a/ Can someone tell me if this script is sound?
b/ How can I test it before inputting to pages?
<script language=javascript>
//check if the referrer of the page is not my site
if( document.referrer!= 'http://domain/dir/' )
{
//redirect to my index page
location.replace ('http://domain/dir/index_file.htm');
</script>
Thank-you, Richard
[edited by: houseofstrauss at 4:02 pm (utc) on Oct. 4, 2003]
I have a strong feeling that
1) document.referrer is not recognized by all browsers, just some specific Netscape versions as i recall.
2) your URL's will be deleted as they are against the forum TOS ;)
Before your url's got deleted (writing in past tense, as they will be) i got a page saying "this page should redirect" - it didn't. IE6
/claus
If document referer is not the way to go do you have any other suggestions please. Php is not offered by my host or I would use session redirect. However, I do have cgi access on another server, but I cant call scripts from SSI
Thanks Richard
document.referrer is not recognized by all browsers, just some specific Netscape versions as i recall.
According to the O'Reilly JavaScript book, document.referrer was not recognized by IE3 -- that may be what you're remembering, claus.
For today's purposes, document.referrer is a standard and widely supported property. It's just that the content of that property is not very reliable -- but that's a different browser issue than whether javascript support exists for the property.
<added>
O'Reilly actually suggests a script for this purpose -- redirecting everyone to your home page if the referrer is not one of your pages.
if {document.referrer.indexOf("mysite.com") == -1}
window.location = "http://mysite.com/index.html";
However, as you know and they remark, this is not a "serious" measure and will not work in all cases. It will also keep someone from viewing your internal pages if they have javascript turned on but their browser is not sending an accurate referrer for any reason.
</added>
I think i got this totally wrong. At least, right now, i really hope so. On second thoughts i'm actually involved in a project where that property is widely used, i just didn't think about it for some reason.
As i recall it now, there was some issues related to Javascript 2 support, anyway, for that project the problem was solved somehow (it's a couple of years back) - now i have to find out how it was done, and what this means for the outcome of this project as well as another i'm currently working on. I simply need to be able to use this property or identify it another way. Thanks a lot for mentioning this.
/claus
if {document.referrer.indexOf("mysite.com") == -1}Thanks for this snippet Tedster, It does create the divert I wanted although, being javascript it is not water tight. Another isssue that occured to me is how SEs handle pages with this type of direct; my pages are real content pages that are to be viewed in an iframe (that's the reason for redirect)placed in the main index page. I do want them indexed and not filtered out as a cloaking page etc. Do you know of any downside using this redirect?
window.location = "http://mysite.com/index.html";
Richard
if (top.frames.length == 0)
or, shorter:
if (top == self).
If true, make javascript build your frameset.
You really should not use document.referrer. It can be filtered by firewalls, and some browsers can be set not to send it. In those cases, using the snippet in msg #5 could cause an endless loop...
Regards, Richard
So, on the pages to be framed, put this piece of script in the <head> section:
<script type="text/javascript">
if (top == self) location = "indexpage.html?ifr=if1&ifrloc=" + escape('this_page.html');
</script> 'this_page.html' being the file name of the page, 'if1' the name of the iframe it should be put into.
On indexpage.html:
<iframe src="default_page1.html" name="if1" id="if1" width="200" height="200"></iframe>
<br>
<iframe src="default_page2.html" name="if2" id="if2" width="200" height="200"></iframe>
<script type="text/javascript">
var locs = location.search;
if (locs && locs.length > 0) {
var ifr = locs.substr(5, 3);
var ifrloc = unescape(locs.substr(16));
window.frames[ifr].location = ifrloc;
}
</script> Make sure the script comes after the iframe-tags.
It may not be the most elegant scripting, but it seems to work in major browsers (javascript enabled, of course).
Thanks Richard