如何在另一个框架中创建函数? [英] How can I create a function in another frame?

查看:90
本文介绍了如何在另一个框架中创建函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方生成的框架集(来自帮助系统),并且可以向具有我的内容页面的子帧中添加代码.我无法修改其他框架集内容,因为它是由构建过程生成的.

I have a 3rd party generated frameset (from a help system) and I can add code to the subframe that has my content pages. I cannot modify the other frameset content since it is generated by a build process.

我的每个HTML内容页面上都有一些js代码myhandler(),这些代码已加载到子框架中.当顶部框中发生动作时,触发此myhandler()调用.

I can have some js code myhandler() on each of my HTML content page that gets loaded into the subframe. This myhandler() is triggered to be called when an action occurs in the top frame.

我想在子帧中创建该函数,但将其归父(顶部)帧所有,以便仅创建一次,这意味着我可以测试top.myhandler是否已设置,是否可以重用它

I would like to create the function in the subframe but have it owned by the parent (top) frame, so that it only gets created once meaning I can test if top.myhandler is already set and if so it just reuses it.

当子帧加载了不同的内容HTML时,当父操作触发对其调用时,将无法再访问在上一个内容页面中创建的myhandler()函数.

When the subframe is loaded with different content HTML then the myhandler() function that was created in the previous content page is no longer accessible when the parent action triggers the call to it.

是否可以在javascript中为一个框架在另一个框架中创建函数?还是对此有其他解决方案?

Is this possible in javascript for a frame to create a function in another frame? Or is there another solution to this?

推荐答案

跨框架javascript有1个条件:所有涉及的页面(在不同框架中)必须在同一域中.如果页面位于不同的域或子域上,出于安全目的,将限制跨框架javascript访问.

There is 1 condition for cross-frame javascript: All involved pages (in different frames) need to be on the same domain. If pages are on different domains or subdomains - cross-frame javascript access will be restricted for security purposes.

假设您有一个包含2页的框架集:index.html和index2.html

Lets suppose you have a frameset with 2 pages: index.html and index2.html

如果这些页面位于相同的域上-它们将共享相同的javascript名称空间,即,从index2.html可以访问链接到index.html的所有javascript库,反之亦然.

If these pages are on the same domain - they will share the same javascript namespace, i.e. all javascript libraries linked to index.html will be accessible from index2.html and versa versa.

如果页面位于同一域的不同子域上,则可以将页面升级几个级别.例如:

If pages are located on different subdomains of the same domain - it is possible to step several levels up. For example:

frame1 contains:    http://myrepo.site.com/index.html
frame2 contains:    http://another.site.com/index2.html

您可以同时设置两者(在JavaScript中):document.domain = "site.com" 之后-两个页面上的javascript代码将开始相互竞争,因为这些页面位于同一域中.

you can set for both (in JavaScript): document.domain = "site.com" After that - javascript code on both pages will start seing each other, same as these pages are on the same domain.

这篇关于如何在另一个框架中创建函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆