Is it possible to override javascript in an iFrame

2019-05-11 20:58发布

问题:

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

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

回答1:

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.



回答2:

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); }


回答3:

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;
 }
}


回答4:

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