2012-01-19 27 views
3

我在我們的觸摸屏應用程序中使用Telerik RadSpell控件。我設法正確的樣式,但是織補物品使用window.alert和window.confirm提示用戶,如果他們想保持更改等。是否有可能從家長的iFrame覆蓋JavaScript?如果是這樣如何?

我想禁用這些警報,而不必拆開並修改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 Page Confirm

是否有可能從父級重寫iframe中的JavaScript?如果是這樣如何?

+0

我應該補充一點,應用程序不會使用警報或確認任何地方,因爲這些對話框對於觸摸屏來說是可怕的。 –

回答

3

我剛剛分享的第一個論壇,它演示瞭如何重寫cancelHandler和隱藏確認對話框一個簡單的解決方案。

爲了您的方便,我粘貼下面的解決方案:

我會提出一個更簡單的方法來禁用彈出式窗口,它是覆蓋cancelHandler功能。要做到這一點請遵循以下步驟:

1)在Web應用程序的根目錄下創建一個JS文件名爲dialog.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)測試溶液。

我希望這會有所幫助。

+0

太棒了!謝謝! –

+0

甜,這完美的作品!如果你不介意我問,你可以在哪裏查看整個js文件? –

+0

@RumenJekov你能幫助我在父窗口的iframe中使用alert()嗎? –

1

您可以使用javascript IFF獲取對innerwindow的引用,該框架來自與父級相同的確切域。

//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); } 
+0

有一個警告,您需要在iframe加載後執行此操作,因此您需要掛鉤iframe元素的'onload'。 –

1

您應該能夠:

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

而不是null檢查,你應該覆蓋iframes的onload handler中的函數 –

0

您可以在以下資源,這可能是你的方案有幫助看一看: http://www.telerik.com/community/forums/aspnet-ajax/spell/how-do-i-turn-off-the-confirm-dialog.aspxhttp://www.telerik.com/help/aspnet-ajax/spell-client-check-finished.html

他們展示如何刪除RadSpell確認和警報彈出窗口。

最好的問候, 瘤胃

+0

嗨Rumen,第一個鏈接實際上是我在第一次遇到這個問題時寫的一篇文章,我發現實現該解決方案的過程是,像大多數telerik控件的黑客,在實踐中非常困難。不幸的是,第二個鏈接中的事件僅適用於最終的「拼寫檢查完成」消息,而不適用於內置於控件中的其他2個警報和1個確認對話框。因此,我試圖破解我的方式! :) –

相關問題