简体   繁体   中英

Top of page from within iFrame - cross domain

Am hoping I haven't overlooked a solution here on SO, but I have been pulling my hair out trying to figure out what I am doing wrong and hoping somebody can see what it is. Here is the configuration:

I have a page that is served on DOMAIN1.COM. For the most part is is a form with a submit button and lots of jQuery. The form can be lengthy and so a user needs to scroll down the page to input form fields. The submit button is at the bottom of this form while the output results are at the top of the form. When the user clicks the submit button he/she cannot see the results because they are off of the top of the page. What I want to do is use jQuery scrollTo() to position to the top of the page once the submit button is clicked.

The problem is that the page above (and submit button) is within an iFrame on DOMAIN2.COM and so when the submit button is clicked I have a cross domain situation that I need to overcome.

I posted a question here , but my question wasn't really accurate. The thread evolved into helpful information and pointed me to a script that uses jQuery postMessage() to communicate cross domain, but I am having a problem implementing the proposed solution. Here's my code:

DOMAIN1.COM (child):

<html>
    <head>
        <!-- Load jquery -->
        <script type="text/javascript" src="./jquery-2.0.3.min.js"></script>

        <!-- Cross-domain scripting goodness (scroll to top) -->
        <script type="text/javascript" src="./jquery.ba-postmessage.js"></script>

        <script type="text/javascript">
            // EVENT Handler - Form Submission
            $(function () {
                $("#btnSubmit").bind('click', function (event) {
                    // Get the parent page URL as it was passed in, 
                    // for browsers that don't support window.postMessage
                    var parent_url = decodeURIComponent(document.location);
                    window.postMessage("scrollTop", parent_url, parent);
                });
            });
        </script>
    </head>

    <body>
        <form>
            ... Lots of form fields resulting in vertical height ...
            <input type="submit" id="btnSubmit" value="Submit" />
        </form>
    </body>
</html>

DOMAIN2.COM (parent):

<html>
    <head>
        <!-- Load jquery -->
        <script type="text/javascript" src="./jquery-2.0.3.min.js"></script>

        <!-- Cross-domain scripting goodness (scroll to top) -->
        <script type="text/javascript" src="./jquery.ba-postmessage.js"></script>

        <script type="text/javascript">
            // Cross-domain - scroll window to top
            window.addEventListener("message", receiveMessage, false);

            function receiveMessage(event) {
                if (event.data == "scrollTop") {
                    window.scrollTo(0, 0);
                }
            }
        </script>
    </head>

    <body>
        <iframe src="http://domain1.com/index.html"></iframe>
    </body>
</html>

Running both pages above on the same domain provides the desired results - clicking submit scrolls the page to the top.

Running on separate domains as documented and coded above, the page does not scroll to top, my output results fail, and jQuery outputs this error message in Firebug:

DataCloneError: The object could not be cloned.

If I change the code to leave out the parent parameter in the page on DOMAIN1.COM like:

var parent_url = decodeURIComponent(document.location);
window.postMessage("scrollTop", parent_url);     //, parent);

Then nothing happens at all and no errors are output. It does not appear that receiveMessage() on DOMAIN2.COM gets called at all.

Can anyone see what I might be doing wrong that prevents this code from working cross domain?

I think it very late now but for others, it may work.

You are almost there; You need to use

window.postMessage("scrollTop","#domain of parent page exact page");

Instead of your code snippet:

 window.postMessage("scrollTop", parent_url, parent);

If at the time the event is scheduled to be dispatched the scheme, hostname, or port of otherWindow's document does not match that provided in targetOrigin, the event will not be sent by Dom. Read documentation here

In Parent Page use following code snippet.

window.addEventListener("message", receiveMessage, false);
function receiveMessage(event)
{
  if (event.origin !== "#domain of iframe")
    return;
  if (event.data == "scrollTop"){
  window.scrollTo(0,0);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM