CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Building Interactive UIs with ASP.NET Ajax: Rebinding Client-Side Events After a Partial Page Postback
  • Speed Up Repetitive Insert, Update, and Delete Query Statements
  • Binding Data to Silverlight 4.0 Controls Using ASP.NET MVC Framework 2.0
  • ADO.NET Data Services in the .NET Framework

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Other Programming > Scripting - Client Side
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    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.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old April 10th, 2009, 08:18 AM
    tcdarow tcdarow is offline
    Junior Member
     
    Join Date: Apr 2009
    Posts: 3
    tcdarow is an unknown quantity at this point (<10)
    [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
    Reply With Quote
      #2    
    Old April 10th, 2009, 09:28 AM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,597
    PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)
    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.
    Reply With Quote
      #3    
    Old April 16th, 2009, 03:09 AM
    tcdarow tcdarow is offline
    Junior Member
     
    Join Date: Apr 2009
    Posts: 3
    tcdarow is an unknown quantity at this point (<10)
    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); 
    }
    This results in an infinite loop. Probably this is being caused by:

    Code:
    current =  current.parent;
    Any help?
    Reply With Quote
      #4    
    Old April 16th, 2009, 06:57 AM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,597
    PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)
    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.
    Reply With Quote
      #5    
    Old April 16th, 2009, 07:05 AM
    tcdarow tcdarow is offline
    Junior Member
     
    Join Date: Apr 2009
    Posts: 3
    tcdarow is an unknown quantity at this point (<10)
    Re: Javascript Referer Url Iframes

    Yes, thats the solution. Thank you very much!
    Reply With Quote
      #6    
    Old April 16th, 2009, 08:08 AM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,597
    PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)
    Re: Javascript Referer Url Iframes

    You're most welcome!
    __________________
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.
    Reply With Quote
      #7    
    Old April 17th, 2009, 02:54 AM
    dannystommen dannystommen is offline
    Member +
     
    Join Date: Sep 2008
    Location: Netherlands
    Posts: 809
    dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)
    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?
    Reply With Quote
      #8    
    Old April 17th, 2009, 06:24 AM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,597
    PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)
    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.
    Reply With Quote
      #9    
    Old April 17th, 2009, 06:26 AM
    dannystommen dannystommen is offline
    Member +
     
    Join Date: Sep 2008
    Location: Netherlands
    Posts: 809
    dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)
    Re: [RESOLVED] Javascript Referer Url Iframes

    Do you know if this is possible using C# asp.net instead of javascript?
    Reply With Quote
      #10    
    Old April 17th, 2009, 08:09 AM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,597
    PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)
    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.
    Reply With Quote
      #11    
    Old April 17th, 2009, 08:15 AM
    dannystommen dannystommen is offline
    Member +
     
    Join Date: Sep 2008
    Location: Netherlands
    Posts: 809
    dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)
    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.
    Reply With Quote
      #12    
    Old April 17th, 2009, 08:34 AM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,597
    PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)
    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.
    Reply With Quote
      #13    
    Old April 17th, 2009, 08:41 AM
    dannystommen dannystommen is offline
    Member +
     
    Join Date: Sep 2008
    Location: Netherlands
    Posts: 809
    dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)dannystommen is a glorious beacon of light (400+)
    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:
    Originally Posted by PeejAvery View Post
    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>
    I found another solution that does the same as the solution you provided, namely just a single line of code
    Code:
    top.location.href
    Too bad this gives the same permission error.
    Reply With Quote
      #14    
    Old April 17th, 2009, 08:47 AM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,597
    PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)PeejAvery has a reputation beyond repute (3000+)
    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.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Other Programming > Scripting - Client Side


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:31 AM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.