是否可以从父级覆盖 iFrame 中的 javascript?如果是这样怎么办?

发布于 2024-12-28 04:13:00 字数 1169 浏览 2 评论 0原文

我在我们的一款触摸屏应用程序中使用 Telerik RadSpell 控件。我已经设法将其样式设置得恰到好处,但是该死的东西使用 window.alert 和 window.confirm 来提示用户是否要保留更改等。

我想禁用这些警报,而不必拆开并修改 telerik 控件。

问题是拼写检查对话框使用 iframe,我似乎无法覆盖 iframe 内的 window.confirm 函数。


用于测试覆盖确认的示例代码。


<!-- mainpage.htm -->
<html>
    <head>
        <script type="text/javascript">
            window.confirm = function(msg){ alert(msg); }
            confirm("Main Page Confirm");
        </script>
    </head>
    <body>
        <iframe src="./iframepage.htm" >
        </iframe>
    </body>
</html>

<!-- iframepage.htm -->
<html>
    <head>
        <script type="text/javascript">
            confirm("iframe confirm");
        </script>
    </head>
    <body>
        Some content.
    </body>
</html>

结果为

Main Page Confirm

iframe 页面确认

是否可以从父级覆盖 iframe 中的 javascript?如果是这样怎么办?

I'm using the Telerik RadSpell control in one of our touchscreen applications. I've managed to style it just right however the darn thing uses window.alert and window.confirm for prompting the user if they want to keep changes etc.

I want to disable these alerts without having to pull apart and modify the telerik controls.

The issue is that the spellcheck dialog uses an iframe and I can't seem to override the window.confirm function inside the iframe.


Sample Code to test overriding confirm.


<!-- mainpage.htm -->
<html>
    <head>
        <script type="text/javascript">
            window.confirm = function(msg){ alert(msg); }
            confirm("Main Page Confirm");
        </script>
    </head>
    <body>
        <iframe src="./iframepage.htm" >
        </iframe>
    </body>
</html>

<!-- iframepage.htm -->
<html>
    <head>
        <script type="text/javascript">
            confirm("iframe confirm");
        </script>
    </head>
    <body>
        Some content.
    </body>
</html>

Results in

Main Page Confirm

iframe Page Confirm

Is it possible to override the javascript in an iframe from the parent? If so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笙痞 2025-01-04 04:13:00

我刚刚在第一个论坛中分享了一个更简单的解决方案,它演示了如何覆盖cancelHandler并隐藏确认对话框。

为了您的方便,我将解决方案粘贴在下面:

我会提出一种更简单的方法来禁用弹出窗口,它是重写 cancelHandler 函数。为此,请按照以下步骤操作:

1) 在 Web 应用程序的根目录中创建一个名为dialog.js 的 JS 文件,并使用以下函数填充该文件:

Telerik.Web.UI.Spell.SpellDialog.prototype.cancelHandler = function (e) {
    if (this._cancel.disabled) {
        return $telerik.cancelRawEvent(e);
    }
    //changes will be applied only if spell handler response is received, text has changed
    //and the user confirms
    this.closeDialog(this._spellProcessor && this._spellProcessor.textChanged() && true);

    return $telerik.cancelRawEvent(e);
}

2) 保存该文件并将 RadSpell 的 DialogsScriptFile 属性设置为指向该文件,例如

3) 测试解决方案。

我希望这有帮助。

I just shared an easier solution in the first forum, which demonstrates how to override the cancelHandler and hide the confirm dialog.

For your convenience I am pasting the solution below:

I would propose an easier way to disable the popup and it is to override the cancelHandler function. To do that follow the steps below:

1) Create a JS file named dialog.js in the root of the web application and populate it with the following function:

Telerik.Web.UI.Spell.SpellDialog.prototype.cancelHandler = function (e) {
    if (this._cancel.disabled) {
        return $telerik.cancelRawEvent(e);
    }
    //changes will be applied only if spell handler response is received, text has changed
    //and the user confirms
    this.closeDialog(this._spellProcessor && this._spellProcessor.textChanged() && true);

    return $telerik.cancelRawEvent(e);
}

2) Save the file and set the DialogsScriptFile property of RadSpell to point to this file, e.g.

3) Test the solution.

I hope this helps.

北座城市 2025-01-04 04:13:00

您可以使用 javascript IFF 获取对内部窗口的引用,该框架与父级框架来自相同的域。

//Get iframe element by getElementById, frames[0], or whatever way you want
var myFrame = document.getElementById("myFrame");
//Get the window of that frame, overwrite the confirm
myFrame.contentWindow.confirm = function(msg){ alert("I overwrote it! : " + msg); }

You can get a reference to the innerwindow using javascript IFF the frame is from the same exact domain as the parent.

//Get iframe element by getElementById, frames[0], or whatever way you want
var myFrame = document.getElementById("myFrame");
//Get the window of that frame, overwrite the confirm
myFrame.contentWindow.confirm = function(msg){ alert("I overwrote it! : " + msg); }
迷雾森÷林ヴ 2025-01-04 04:13:00

您应该能够:

document.getElementById('iframe').contentWindow.confirm = [this is confirm in the iframe];

也许这样的东西可能对您来说效果很好:

document.getElementById('iframe').contentWindow.confirm = window.confirm;

这会将 iframe 的确认链接到父级的确认,如果您已经在父级中对确认进行了一些处理,那么这很好。

请注意,您还需要为可能的未定义对象添加一些处理。

var iframe = document.getElementById('iframe');
//iframe exists
if(iframe){
 var iframe_window = document.getElementById('iframe').contentWindow;
 //window exists (won't if frame hasn't loaded)
 if(iframe_window){
  iframe_window.confirm = window.confirm;
 }
}

You should be able to:

document.getElementById('iframe').contentWindow.confirm = [this is confirm in the iframe];

Perhaps something like this might work nicely for you:

document.getElementById('iframe').contentWindow.confirm = window.confirm;

This would link the confirm of the iframe to the confirm of the parent, which is nice if you already have some handling for confirms in the parent.

Note that you also will want to add some handling for possible undefined objects.

var iframe = document.getElementById('iframe');
//iframe exists
if(iframe){
 var iframe_window = document.getElementById('iframe').contentWindow;
 //window exists (won't if frame hasn't loaded)
 if(iframe_window){
  iframe_window.confirm = window.confirm;
 }
}
只为守护你 2025-01-04 04:13:00

您可以查看以下资源,这可能对您的情况有所帮助:
http ://www.telerik.com/community/forums/aspnet-ajax/spell/how-do-i-turn-off-the-confirm-dialog.aspx

http://www.telerik.com/help/aspnet -ajax/spell-client-check-finished.html

它们展示了如何删除 RadSpell 确认和警报弹出窗口。

此致,
瘤胃

You can take a look at the following resources, which could be helpful for your scenario:
http://www.telerik.com/community/forums/aspnet-ajax/spell/how-do-i-turn-off-the-confirm-dialog.aspx
and
http://www.telerik.com/help/aspnet-ajax/spell-client-check-finished.html

They show how to remove the RadSpell confirm and alert popups.

Best regards,
Rumen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文