当通过LTI在iFrame中加载时,*javascript关键字无效

时间:2022-09-19 09:13:19

My content is loaded in another application via LTI launch. That application is trying to load the content in its iFrame.

我的内容通过LTI启动加载到另一个应用程序中。该应用程序正在尝试在其iFrame中加载内容。

My content has javascript calls which is written as top.setLinkVisibility=setLinkVisibility; top.isDeeplinkSession()

我的内容有javascript调用,写成top.setLinkVisibility = setLinkVisibility; top.isDeeplinkSession()

When these javascript keywords get executed the content is not loaded properly (getting permission denied exception) Error: Permission denied to access property

当这些javascript关键字被执行时,内容未正确加载(获得权限被拒绝异常)错误:访问属性的权限被拒绝

当通过LTI在iFrame中加载时,*javascript关键字无效

This content would be loaded within my application and also out of my application (via LTI launch).

此内容将加载到我的应用程序中,也可以从我的应用程序中加载(通过LTI启动)。

Now we tried to replace top keyword with self and it works fine

现在我们尝试用self取代top关键字,它运行正常

But to make it work for all the content we have to replace the keyword top to self in more than 1000 files and thats really tedious.

但要使其适用于所有内容,我们必须将关键字top to self替换为1000多个文件,这真的很乏味。

Any other way to make the content work ? Replacing top to self is one way .. but any easier way ??

任何其他方式使内容工作?将top替换为self是一种方式..但更简单的方法?

1 个解决方案

#1


1  

No, there is no other way. top automatically refers to the topmost window in the window hierarchy. Since it's read-only (to prevent an iframe from accessing the parent document's window), there's no way to use the window.top property. You will either have to replace all top usages with self (which is not read-only or reserved), or place your entire script in a different scope.

不,没有其他办法。 top自动引用窗口层次结构中最顶层的窗口。由于它是只读的(为了防止iframe访问父文档的窗口),因此无法使用window.top属性。您将要么必须用self替换所有*用法(不是只读或保留),要么将整个脚本放在不同的范围内。

Since the window.top property is a property of the window object, it is automatically a global variable. If you define a variable outside any functions (in the global scope), it'll be a property of the window object, so it's not possible to define var top in the global scope, since that'd have to overwrite the read-only window.top. If you were to use var top inside a function though, it'd work just fine, as can be demonstrated in this example:

由于window.top属性是window对象的属性,因此它自动成为全局变量。如果在任何函数之外定义变量(在全局范围内),它将是window对象的属性,因此不可能在全局范围中定义var top,因为它必须覆盖只读window.top。如果你在函数中使用var top,它可以正常工作,如本例中所示:

function foo() {
    var top = 'test';
    return top;
}
alert(foo()); //Alerts "test"

In the future, it's best to avoid overly generic variable names such as top. They can easily cause problems. Especially when they need to be global variables.

在将来,最好避免使用过于通用的变量名称,例如top。它们很容易引起问题。特别是当他们需要成为全局变量时。

#1


1  

No, there is no other way. top automatically refers to the topmost window in the window hierarchy. Since it's read-only (to prevent an iframe from accessing the parent document's window), there's no way to use the window.top property. You will either have to replace all top usages with self (which is not read-only or reserved), or place your entire script in a different scope.

不,没有其他办法。 top自动引用窗口层次结构中最顶层的窗口。由于它是只读的(为了防止iframe访问父文档的窗口),因此无法使用window.top属性。您将要么必须用self替换所有*用法(不是只读或保留),要么将整个脚本放在不同的范围内。

Since the window.top property is a property of the window object, it is automatically a global variable. If you define a variable outside any functions (in the global scope), it'll be a property of the window object, so it's not possible to define var top in the global scope, since that'd have to overwrite the read-only window.top. If you were to use var top inside a function though, it'd work just fine, as can be demonstrated in this example:

由于window.top属性是window对象的属性,因此它自动成为全局变量。如果在任何函数之外定义变量(在全局范围内),它将是window对象的属性,因此不可能在全局范围中定义var top,因为它必须覆盖只读window.top。如果你在函数中使用var top,它可以正常工作,如本例中所示:

function foo() {
    var top = 'test';
    return top;
}
alert(foo()); //Alerts "test"

In the future, it's best to avoid overly generic variable names such as top. They can easily cause problems. Especially when they need to be global variables.

在将来,最好避免使用过于通用的变量名称,例如top。它们很容易引起问题。特别是当他们需要成为全局变量时。