简体   繁体   English

从同一iframe更改iframe网址

[英]change the iframe url from the same iframe

I need to change the iframe url from the same iframe. 我需要从同一iframe更改iframe网址。 For example: " http://parent.com " is the Parent Domain URL of my website with an http and: " https://iframe.com " is the iframe Domain URL with an https. 例如:“ http://parent.com ”是我的网站的带有http的父域URL,而“ https://iframe.com ”是带有https的iframe域名URL。 The two domains are different. 这两个域是不同的。

Here's the code of the parent window: 这是父窗口的代码:

  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <script>
        function sheck19()
        {
          document.getElementById("myFrame19").height="700";
          document.getElementById('myFrame19').src="https://iframe.com/home.php";
        }
      </script>
    </head>
    <body>
        <iframe id="myFrame19" src="https://iframe.com/index.php" width="620" height="410" frameborder="0"></iframe>
    </body>
  </html>

and here's the code of the iframe document: 这是iframe文档的代码:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <script type="text/javascript">
      function uuui() {
        window.parent.sheck19();
      };
    </script>
  </head>

  <body>
    <input  name="Continuer" type="submit" value="Continuer" onclick="uuui();">
  </body>

</html>

What you want to do is not possible. 您想做什么是不可能的。 If the two domain are different you cannot communicate in the way you're trying. 如果两个域不同,则无法以尝试的方式进行通信。 The Browsers Same-origin policy will prevent it. 浏览器同源策略将阻止该行为。

What you can do is use the window.postMessage API. 您可以使用window.postMessage API。 Read more about that here: https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage . 在此处阅读有关此内容的更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/Window.postMessage It's pretty widely supported: IE8+, FF6+Safari 4+, Chrome 1+, Opera 9.5+ 受到广泛支持:IE8 +,FF6 + Safari 4 +,Chrome 1 +,Opera 9.5+

 otherWindow.postMessage(message, targetOrigin, [transfer]);

Accessing a parent page's URL is not allowed if the iframe and the main page are not in the same (sub)domain. 如果iframe和主页不在同一个(子)域中,则不允许访问父页面的URL。 However, if you just need URL of the main page (ie the browser URL), you can try this 但是,如果您只需要主页的URL(即浏览器URL),则可以尝试此操作

var url = (window.location != window.parent.location) ? document.referrer: document.location;

This has worked for me in the past. 过去这对我有用。

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

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