| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Scripting - Client Side Discuss client-side scripting issues. Client-side scripting such as JavaScript, JScript, and VBScript as well as technologies such as HTML and stylesheets. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
[RESOLVED] Javascript Referer Url Iframes
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 |
|
#2
|
||||
|
||||
|
Re: Javascript Referer Url Iframes
You will have to loop through checking the parent of each window. In theory, the following should work.
Code:
while (document.parent) {
__________________
If the post was helpful...Rate it! Remember to use [code] or [php] tags. |
|
#3
|
|||
|
|||
|
Re: Javascript Referer Url Iframes
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: Code:
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);
}
Code:
current = current.parent; |
|
#4
|
||||
|
||||
|
Re: Javascript Referer Url Iframes
Apparently, the parent object is never traversed. I came up with this work-around.
Code:
<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>
__________________
If the post was helpful...Rate it! Remember to use [code] or [php] tags. |
|
#5
|
|||
|
|||
|
Re: Javascript Referer Url Iframes
Yes, thats the solution. Thank you very much!
|
|
#6
|
||||
|
||||
|
Re: Javascript Referer Url Iframes
You're most welcome!
__________________
If the post was helpful...Rate it! Remember to use [code] or [php] tags. |
|
#7
|
|||
|
|||
|
Re: [RESOLVED] Javascript Referer Url Iframes
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? |
|
#8
|
||||
|
||||
|
Re: [RESOLVED] Javascript Referer Url Iframes
If the domain changes, from the original host, then no, you cannot. That is a security block by the browser.
__________________
If the post was helpful...Rate it! Remember to use [code] or [php] tags. |
|
#9
|
|||
|
|||
|
Re: [RESOLVED] Javascript Referer Url Iframes
Do you know if this is possible using C# asp.net instead of javascript?
|
|
#10
|
||||
|
||||
|
Re: [RESOLVED] Javascript Referer Url Iframes
Remember that ASP.NET is a server-side language, whereas the frames are loaded from the client-side.
__________________
If the post was helpful...Rate it! Remember to use [code] or [php] tags. |
|
#11
|
|||
|
|||
|
Re: [RESOLVED] Javascript Referer Url Iframes
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. |
|
#12
|
||||
|
||||
|
Re: [RESOLVED] Javascript Referer Url Iframes
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.
__________________
If the post was helpful...Rate it! Remember to use [code] or [php] tags. |
|
#13
|
|||
|
|||
|
Re: Javascript Referer Url Iframes
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. Quote:
Code:
top.location.href |
|
#14
|
||||
|
||||
|
Re: Javascript Referer Url Iframes
Ha. I totally forgot about top! Thanks for jogging my memory.
__________________
If the post was helpful...Rate it! Remember to use [code] or [php] tags. |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|