Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Detecting an http address in a frame

    Hi All,

    If I ask this correctly it may be understood.

    I understand for security you cannot invoke cross domain scripting. In this case having code on a thank you page of a form executing a script on a PC's locally launched .html web page.

    However. If you have a .html web page launched locally on a PC that has an iFrame showing a form that can be submitted. When the form is submitted and it's thank you page is displayed, is it possible using a javascript to detect the thank you page's http address and display it in an input field.

    Example being the original forms http in the iFrame was say http://somedomain.xxx/myform.html.

    When submitted the form displays the thank you page who's address would be say http://somedomain.xxx/thankyou.html

    Can that.... http://somedomain.xxx/thankyou.html be read in some way and displayed in an input field to show that thank you page actually loaded.

    I know someone is going to say, but you can see it loaded. Yes but I am wondering from a code point of view if a script can see the address in the iFame and display it in the input field so that other scripts on the PC's web page can use that knowing it showed to do something else.

    Martin.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    you can only set the src of iframe, not get it.
    you can use postMessage to send data from the form page to the top, but you need to set that up on the form page itself, not the framing page.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    You can read the iframe src but it will always return the original src even if the page in the iframe changes.

    Code:
    <iframe src="http://www.cnn.com" onload="alert(this.src)"></iframe>

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks to both of you for the comments.

    I had tried some code and always received the original src, I will look at the postMessage idea to see if I can adapt it for what I was trying to do.

    Martin.

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    The easiest way is for the thankyou page to call a routine in the parent:

    Code:
    if( window.self !== window.parent && typeof window.parent.myFunc == 'function' )
      window.parent.myFunc( window.location.href );
    where myFunc is a global function in the parent document.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    The OP said the main page will be launched locally from the desktop and the iframe src is on some domain. So you the iframe document has no access to the parent and vice versa.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by glenngv View Post
    The OP said the main page will be launched locally from the desktop and the iframe src is on some domain. So you the iframe document has no access to the parent and vice versa.
    postMessage should still work across protocols...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    Yeah but the iframe page must do the postMessage call to the parent. I doubt that the iframe site that the OP is using is doing that. In order for the postMessage to be successful, the 2 parties need to have an agreement and allow each other to communicate.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by glenngv View Post
    Yeah but the iframe page must do the postMessage call to the parent. I doubt that the iframe site that the OP is using is doing that. In order for the postMessage to be successful, the 2 parties need to have an agreement and allow each other to communicate.
    ahh, for sure. good point, thanks for pointing that out. we don't know if that's the case or not, but it should have been mentioned.

    considering the OP further, i would imagine that a simple form onsubmit() event would provide the temporal distinction needed.

    even though OP asked for the url, he didn't mention error handling, so he describes a need for a simple event, and that event is readily available.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #10
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by glenngv View Post
    The OP said the main page will be launched locally from the desktop and the iframe src is on some domain. So you the iframe document has no access to the parent and vice versa.
    Looks like I misinterpreted. Tests suggest that 'local' needs to be a local http server, not file:///. When the latter was specified as the second param to parent.postMessage, nothing was received.

    Here's a test page that can be run from localhost. The destination is just doing parent.postMessage( location.href, "http://localhost" );
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Parent  Doc</title>
    
    </head>
    
    <body>
    
    <iframe src='http://veryuseful.info/test/formchild.htm'></iframe>
    
    <p><span id='remoteDomain'></span>
    
    <script type = "text/javascript">
    
    window.addEventListener( 'message', getDomain, false );
    
    function getDomain( evt )
    { 
      document.getElementById( 'remoteDomain' ).innerHTML = "Form received by: " + evt.data;
    }
    
    </script>
    
    </body>
    </html>

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    @Logic Ali, as I mentioned, using postMessage requires the external domain to issue that call. If you don't have control of that domain, then that will not work.

    I assume by "launching locally on a PC", it means running in file:// protocol. If so, then we can do an HTA page. Try saving this html with .hta extension:

    Code:
    <html>
    <head>
    </head>
    <body>
    <iframe src="http://wikipedia.org" onload="alert(this.contentWindow.location.href)" width="800" height="600"></iframe>
    </body>
    </html>

  • #12
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by glenngv View Post
    @Logic Ali, as I mentioned, using postMessage requires the external domain to issue that call. If you don't have control of that domain, then that will not work.
    I think we all knew that from the outset, and in this instance I understood it not to be an issue.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •