简体   繁体   English

iframe网址更改时如何更改包装器

[英]How do I change wrapper when iframe url changes

I have a page with an iframe. 我有一个带有iframe的页面。 My page has a banner/header that displays on the page. 我的页面上显示了横幅/标题。 After the user clicks submit in the iframe form. 用户单击后,以iframe表单提交。 I want the banner to go away. 我希望横幅消失。

http://todayshousingmarketvalues.com/ http://todayshousingmarketvalues.com/

This is the page. 这是页面。 If you put in an address and click submit you'll see what I need. 如果您输入地址并单击提交,您将看到我所需要的。 Make up an address. 组成一个地址。 You don't need to use your own. 您不需要使用自己的。 Nothing happens if you don't subscribe. 如果您不订阅,则不会发生任何事情。

You can handle iframe's onload event. 您可以处理iframe的onload事件。 If both iframe and parent page are on the same domain - you can check iframe's contentWindow.location.href value and if it changes - that means user performed an action - hide the banner. 如果iframe和父页面都在同一个域上-您可以检查iframe的contentWindow.location.href值,并且如果它发生更改-这表示用户已执行操作-隐藏横幅。

If domains are not the same - this approach won't work since access to that property will be denied. 如果域相同-这种方法将行不通,因为将拒绝对该属性的访问。 In that case you can set a flag on the parent, marking initial load of iframe. 在这种情况下,您可以在父项上设置一个标志,以标记iframe的初始加载。 And change the flag on consecutive loads, eg call something like in iframe onload: 并在连续加载时更改标志,例如,在iframe onload中调用以下代码:

    var initialLoad = true;

    function checkURL() {
       if (initialLoad) {
           initialLoad = false;
        } else {
          document.getElementById("banner").style.display="none";
        }
     }

Demo: http://jsfiddle.net/MMa9w/ see the banner, then type anything and click the button 演示: http : //jsfiddle.net/MMa9w/看到横幅,然后输入任何内容并单击按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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