Click to See Complete Forum and Search --> : [RESOLVED] Javascript Referer Url Iframes
tcdarow
April 10th, 2009, 09:18 AM
Hello,
I would like to retrieve the referrer Url from the top document. I have a iframe with content. This iframe can be placed directly in a html document or it can be placed iframe. The amount of nested iframes is unknown.
If I use document.referrer, it gives me the referrer of the parent of the iframe which in some cases is a iframe itself.
Any help would be appreciated
PeejAvery
April 10th, 2009, 10:28 AM
You will have to loop through checking the parent of each window. In theory, the following should work.
while (document.parent) {
tcdarow
April 16th, 2009, 04:09 AM
Thank you for your reply.
document.parent always returns undefined.
I tried various methods to loop trough the frames but till now I haven't found the solution.
I know I have to loop trough the Iframes.
I tried the following:
var Iframes = 0;
var current = window.parent;
alert(current.frames.length);
Iframes = current.frames.length;
while ( Iframes > 0)
{
current = current.parent;
Iframes = current.frames.length;
alert(Iframes);
}
This results in an infinite loop. Probably this is being caused by:
current = current.parent;
Any help?
PeejAvery
April 16th, 2009, 07:57 AM
Apparently, the parent object is never traversed. I came up with this work-around.
<script type="text/javascript">
var prevLocation = "";
var trueParent = parent;
for (var i = 0; i < 100; i++) { // avoid infinite loop...just in case something goes wrong
trueParent = trueParent.parent;
if (trueParent.window.location == prevLocation) {break;}
}
alert(trueParent.window.location);
</script>
tcdarow
April 16th, 2009, 08:05 AM
Yes, thats the solution. Thank you very much!
PeejAvery
April 16th, 2009, 09:08 AM
You're most welcome! :wave:
dannystommen
April 17th, 2009, 03:54 AM
This thread is started by my colleague (who has the day off today), but the solution does not work.
When he tested it, it worked fine because all the iframes where on the same server. When taking it into production last night, the code failed.
The iframes are on different servers (which we don't have access to) and now the script results in an 'permission denied' error. The is probably caused by the fact that javascript cannot go cross domains.
Is there anyway to solve this? Or is it simply not possible?
PeejAvery
April 17th, 2009, 07:24 AM
If the domain changes, from the original host, then no, you cannot. That is a security block by the browser.
dannystommen
April 17th, 2009, 07:26 AM
Do you know if this is possible using C# asp.net instead of javascript?
PeejAvery
April 17th, 2009, 09:09 AM
Remember that ASP.NET is a server-side language, whereas the frames are loaded from the client-side.
dannystommen
April 17th, 2009, 09:15 AM
I am aware of that.
We only need to need to know the 'top' referrer. It doesn't matter wether this information is retrieved at client- or serverside.
In asp.net in case use 'Request.ServerVariables["HTTP_REFERER"]' to get the referrer which works, but I cannot loop through till I reached the top document. That means, I don't know how or that is even possible or not.
PeejAvery
April 17th, 2009, 09:34 AM
You may be aware of the differences, but you are missing what I am saying.
The server-side can only interact with the page that is coming from that server load. Since the other frames aren't part of that initiated server instance, then there is no way you can access them from the server-side.
dannystommen
April 17th, 2009, 09:41 AM
I get the point. Too bad!
Then I have some additionall information for people who need the top referrer, but don't have the problem with the cross domain.
Apparently, the parent object is never traversed. I came up with this work-around.
<script type="text/javascript">
var prevLocation = "";
var trueParent = parent;
for (var i = 0; i < 100; i++) { // avoid infinite loop...just in case something goes wrong
trueParent = trueParent.parent;
if (trueParent.window.location == prevLocation) {break;}
}
alert(trueParent.window.location);
</script>
I found another solution that does the same as the solution you provided, namely just a single line of code
top.location.href
Too bad this gives the same permission error.
PeejAvery
April 17th, 2009, 09:47 AM
Ha. I totally forgot about top! Thanks for jogging my memory.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.