2

我的网站上有一个注册表单,iframe我想在 URLiframe更改时创建重定向(当用户成功注册时)。

这两个站点是跨域的,我知道几乎不可能跨域拉 url,但是有解决方法吗?我知道 src 不会改变,我正在考虑使用onload(),当iframe第二次加载时(当用户成功注册时),执行一个函数来重定向到感谢页面。

4

1 回答 1

0

这是一个使用 javascript ' Porthole ' 的示例。

这是可能的,但请记住 iframe 的安全问题。解决方案:如果您可以控制原始页面 + iframe,则可以通过在两侧实现一些 javascripts 来“欺骗”浏览器。

首先在两个域上创建一个“代理”页面。将其命名为“proxy.html”或其他名称(注意:您必须使用“porthole.min.js”,您可以从底部的来源中获取)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <!-- Replace the url with your own location -->
        <script type="text/javascript" src="porthole.min.js"></script>
        <script type="text/javascript">
            window.onload=function(){ Porthole.WindowProxyDispatcher.start(); };
        </script>
    </head>
    <body>
    </body>
</html>

父页面上:(参考iframe proxy.html 页面)

<script type="text/javascript" src="porthole.min.js"></script>
<iframe id="guestFrame" name="guestFrame" src="http://iframe.otherdomain.com/"></iframe>

<script type="text/javascript">
var iframeDomain = 'http://iframe.otherdomain.com';
var redirectUrl = 'http://www.mydomain.com/redirect-to/signed-up';

function onMessage(messageEvent) {
    if (messageEvent.origin == iframeDomain) {
        if (messageEvent.data["action"]
         && messageEvent.data["action"] == 'signed-up) {
            window.location.href = redirectUrl; // The final action!
            // This is the eventual redirect that will happen
            // once your visitor has signed-up within the iframe
        }
    }
}
var windowProxy;
window.onload=function(){ 
    // Create a proxy window to send to and receive messages from the iFrame
    windowProxy = new Porthole.WindowProxy(
        'http://iframe.otherdomain.com/proxy.html', 'guestFrame');

    // Register an event handler to receive messages;
    windowProxy.addEventListener(onMessage);
};
</script>

iframe 页面上(参考proxy.html 页面)

<script type="text/javascript">
var windowProxy;
window.onload=function(){ 
    // Create a proxy window to send to and receive messages from the parent
    windowProxy = new Porthole.WindowProxy(
        'http://www.mydomain.com/proxy.html');

    // Register an event handler to receive messages;
    windowProxy.addEventListener(function(event) { 
        // handle event (not used here, the iframe does not need to listen)
    });
};
</script>

您可以从 iframe 向父页面发送带有 javascript 的消息(也可以通过其他方式)。如果您在 iframe 域中仅使用 1 个 javascript,则可以执行以下操作以在 url 更改为特定内容时向父框架发送消息,例如“signed-up.php”(未经测试,但您将明白了)

<script type="text/javascript">
window.onload=function(){
    if(window.location.href.indexOf("signed-up.php") > -1) {
        windowProxy.post({'action': 'signed-up'}); // Send message to the parent frame
        // On the parent page, the function 'onMessage' is triggered.
    }
};
</script>

资料来源:

于 2014-06-20T22:33:48.570 回答