简体   繁体   English

帧之间如何通信?

[英]How to communicate between frames?

I'm maintaining an application that goes sort of like this:我正在维护一个类似这样的应用程序:

There is a Page A with a Frame that shows Page B. Now page B is part of a completely different product in a separate domain.有一个带有显示页面 B 的框架的页面 A。现在页面 B 是单独域中完全不同产品的一部分。

Now, they want that when an option in B is clicked, the WHOLE page is redirected to another page in A. The problem is that the url of A is something like www.client.A.com/Order/Details/123 , and when we click in be it should redirect to something like www.client.A.com/Order/Edit/123 but B doesn't know anything about A. It doesn't know which order # is currently selected or anything about A. Page A who has the frame B does know it.现在,他们希望当点击 B 中的一个选项时,整个页面被重定向到 A 中的另一个页面。问题是 A 的 url 类似于www.client.A.com/Order/Details/123 ,并且当我们点击时,它应该重定向到类似www.client.A.com/Order/Edit/123东西,但 B 对 A 一无所知。它不知道当前选择了哪个订单 # 或有关 A 的任何信息。拥有框架 B 的页面 A 确实知道这一点。

For now my solution has been to just redirect to the AllOrders so something like client.MyCompany/Orders现在我的解决方案是重定向到 AllOrders 所以像 client.MyCompany/Orders

but since B doesn't know which client is calling it (its a multi-tenant app), I'll add it in the webconfig.但由于 B 不知道哪个client端正在调用它(它是一个多租户应用程序),我将它添加到 webconfig.xml 中。 (so each client has its own webconfig with a different value). (因此每个客户端都有自己的具有不同值的 webconfig)。

I dont find this solution optimal but I can't think of anything else!我不认为这个解决方案是最佳的,但我想不出别的了! I already tried putting the needed url in page A in a hidden Div (since A does know all the info) and then trying to read the whole DOM of the page from B to find it.... unfortunately I can only get access to Frame B's DOM... (I tried with jquery).我已经尝试将页面 A 中所需的 url 放在隐藏的 Div 中(因为 A 确实知道所有信息),然后尝试从 B 读取页面的整个 DOM 以找到它......不幸的是我只能访问框架 B 的 DOM...(我用 jquery 试过)。

I know frames are evil, but this is how it is written... any ideas?我知道框架是邪恶的,但它是这样写的……有什么想法吗?

Thanks!谢谢!

If the parent page A and the iframe page B are in different domains, you will not be able to access methods or fields via B's parent property, nor will script in A be able to reach into B's content, nor will you be able to share global variables between A and B. This boundary placed between page A and page B is a key part of the browser security model.如果父页面 A 和 iframe 页面 B 在不同的域中,您将无法通过 B 的父属性访问方法或字段,A 中的脚本也无法访问 B 的内容,也无法共享A 和 B 之间的全局变量。这个位于页面 A 和页面 B 之间的边界是浏览器安全模型的关键部分。 It's what prevents evil.com from wrapping your online bank web page and stealing your account info just by reading the internal variables of the javascript of the bank's web page.这就是防止 evil.com 仅通过读取银行网页的 javascript 的内部变量来包装您的在线银行网页并窃取您的帐户信息的原因。

If you have the luxury of requiring the latest generation of browsers, you can use the postmessage technique mentioned in one of the other answers here.如果您有必要使用最新一代的浏览器,则可以使用此处其他答案之一中提到的 postmessage 技术。 If you need to support older browsers, you may be able to pass small amounts of information using cross-domain client scripting techniques in the browser.如果您需要支持较旧的浏览器,您可以在浏览器中使用跨域客户端脚本技术传递少量信息。 One example of this is to use iframes to communicate info between the outer page A and the inner page B. It's not easy and there are many steps involved, but it can be done.其中一个例子是使用 iframe 在外页 A 和内页 B 之间传递信息。这并不容易,涉及的步骤很多,但可以做到。 I wrote an article on this awhile ago.不久前我写了一篇关于这个的文章

You will not be able to monitor clicks in B's iframe from the parent page A. That's a violation of browser security policies at multiple levels.您将无法从父页面 A 监控 B 的 iframe 中的点击。这违反了多个级别的浏览器安全策略。 (Click hijacking, for one) You won't be able to see when B's URL changes - A can write to the iframe.src property to change the URL, but once the iframe.src points to a different domain than A's domain, A can no longer read the iframe.src property. (点击劫持,例如)您将无法看到 B 的 URL 何时更改 - A 可以写入 iframe.src 属性来更改 URL,但是一旦 iframe.src 指向与 A 的域不同的域,A无法再读取 iframe.src 属性。

If A and B are in different subdomains of the same root domain, you may have an opportunity to "lower" the domain to a common root.如果 A 和 B 位于同一根域的不同子域中,您可能有机会将域“降低”为公共根。 For example, if the outer page A is hosted in subdomain A.foo.bar.com, and B is hosted in subdomain foo.bar.com, then you can lower the domain in page A to foo.bar.com (by assigning window.domain = "foo.bar.com" in A's script).例如,如果外页面 A 托管在子域 A.foo.bar.com 中,而 B 托管在子域 foo.bar.com 中,那么您可以将页面 A 中的域降低到 foo.bar.com(通过分配window.domain = "foo.bar.com" 在 A 的脚本中)。 Page A will then behave as a peer of page B and the two can then access each other's data as needed, even though A is technically being served from a different domain than B. I wrote an article on domain lowering , too.然后页面 A 将表现为页面 B 的对等方,然后两者可以根据需要访问彼此的数据,即使从技术上讲 A 是从与 B 不同的域提供服务的。我也写了一篇关于域降低的文章。

Domain lowering can only peel off innermost subdomains to operate in the context of a root domain.域降低只能剥离最内部的子域以在根域的上下文中运行。 You can't change A.foo.bar.com to abc.com.您不能将 A.foo.bar.com 更改为 abc.com。

There is also a slight risk in lowering domains to a common root domain.将域降级为公共根域也存在轻微风险。 When you operate your page in its own subdomain, your html and script are segregated from the other subdomains off the common root domain.当您在自己的子域中操作页面时,您的 html 和脚本与公共根域之外的其他子域隔离。 If a server in one of the other subdomains is compromised, it doesn't really affect your html page.如果其他子域之一中的服务器受到威胁,它不会真正影响您的 html 页面。

If you lower your page's domain to the common root domain, you are exposing your internals to script running on the common root domain and to script from other subdomains that has also lowered its domain to the common root.如果您将页面的域降低到公共根域,那么您将内部结构暴露给在公共根域上运行的脚本以及来自其他子域的脚本,这些子域也将其域降低到了公共根。 If a server in one of the other subdomains is compromised, it will have access to your script's internals and therefore it may have compromised your subdomain as well.如果其他子域之一中的服务器受到威胁,它将可以访问您脚本的内部结构,因此它也可能已经破坏了您的子域。

in case the page & frame are not on the same domain, you'll have to use postmessage as the same-domain policy prohibits normal javascript-communication between pages/frames of different domains because of security concerns.如果页面和框架不在同一个域中,您将不得不使用postmessage,因为出于安全考虑,同域策略禁止不同域的页面/框架之间的正常 javascript 通信。

postmessage is part of html5 and works in all modern browsers (including IE8) . postmessage 是 html5 的一部分,适用于所有现代浏览器(包括 IE8) if you need support for older browsers (specifally IE6/7), you could use the jQuery postmessage plugin (which transparently falls back to some nice hash-tag trickery for older browsers).如果您需要支持较旧的浏览器(特别是 IE6/7),您可以使用jQuery postmessage 插件(对于较旧的浏览器,它可以透明地回退到一些不错的哈希标签技巧)。

and as a sidenote: not sure if frames are evil, there are some problems (usability, SEO, ...) related to them, but i did some research and most of these can be tackled i think.作为旁注:不确定框架是否邪恶,有一些与它们相关的问题(可用性、搜索引擎优化……),但我做了一些研究,我认为其中大部分都可以解决

If you want to communicate between frames in javascript you can use 'parent':如果你想在 javascript 中的框架之间进行通信,你可以使用“父”:

If frame A has a variable value, eg:如果帧 A 具有变量值,例如:

var orderNo = 2;

For frame B to read it it would refer to对于框架 B 阅读它会参考

var frameA_orderNo = parent.frames[0].orderNo;

(assuming that frame A is the first frame declared) (假设帧 A 是声明的第一帧)

So you can set up global variables within each frame that the other frame can read and therefore you can get the order # in old fashioned javascript (never tried it in jquery).因此,您可以在每个帧中设置其他帧可以读取的全局变量,因此您可以在老式 javascript 中获取订单 #(从未在 jquery 中尝试过)。

Wow frames - never thought I'd think about them again.哇帧 - 从没想过我会再次考虑它们。

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

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