This is a migrated thread and some comments may be shown as answers.

Javascript File and iFrame

4 Answers 140 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 25 Mar 2008, 01:54 AM
Hi,
How can I attach a JS file to the iframe of the editor so that I can include javascript calls in my content? 

For example, I have a simple "show/hide div" javascript function in a file called scripts.js. I have content, managed via the editor, that contains the following HTML:
<a href="#" onclick="toggle_visibility('foo');">CLICK HERE</a>
<div id="foo" style="DISPLAY: none">MY CONTENT</div>
This code simply shows or hides the div named "foo".

The problem is that when editing the HTML in the editor, the page quickly displays a javascript error, breaking the editor. My best guess is that the iframe used by the editor cannot see the linked JS file. So it seems that attaching the JS file to the editor or iframe somehow would solve this.

Make sense?
Thanks for any help,
Joe

4 Answers, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 28 Mar 2008, 05:05 PM
Hi Joe,

You can achieve your scenario using the solution below that imports the external js code in the editor's content area:

    <script id="PageJavaScript" language="javascript" type=text/javascript>
    function test1()
    {
        alert("test");
    }
    function test2()
    {
        alert("test2");
    }
    </script>   
</head>
<body>
<script type="text/javascript">
function OnClientLoad(editor, sender)
{
    var oPageScript = document.getElementById("PageJavaScript");
    var filter  = editor.get_FiltersManager();
    filter.getFilterByName("EncodeScriptsFilter").Enabled = false;
                                   
    var oScript  = "<script defer='false'>\n" + oPageScript.innerHTML + "\n</" + "script>";   
    editor.setFocus();
    alert(editor);
    editor.set_html(editor.get_html(true) + oScript);
}
</script>

<form id="Form1" method="post" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<telerik:RadEditor ID="RadEditor1" AllowScripts="true" OnClientLoad="OnClientLoad" runat="server">
    <Content>
        <img onmouseover="test1();" onmouseout="test2();" src="http://www.telerik.com/r.a.d.controls/Editor/Img/productBox.gif">               
    </Content>
</telerik:RadEditor>

Feel free to modify and enhance the solution to fit your scenario.

Best wishes,
Rumen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Joe
Top achievements
Rank 2
answered on 29 Mar 2008, 02:54 PM
Thanks Rumen.

After applying your solution, it appears that was not my problem after all...

Tested in both IE 6, and Firefox 2, with or without JS calls in the content:
After saving content (postback) the editor gives a JS error fairly frequently when I try to save a second time. I'd say 50% of the time. Here are the two JS errors produced.

Line: 23 _gel("GM71statusmsg") has no properties
Line: 5210 _bf.contentWindow has no properties

Is this a known issue? And if so, any work around?

Thanks
0
Rumen
Telerik team
answered on 31 Mar 2008, 03:11 PM
Hi Joe,

I am not aware of such a problem.

Could you please open a support ticket and send me a sample running project that demonstrates the reported problem? I want to reproduce the issue on my side and examine your code.

After that I will see what is causing the problem and try to provide a solution.

Best regards,
Rumen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Joe
Top achievements
Rank 2
answered on 31 Mar 2008, 03:33 PM
I have several pages that use the editor and I'm only seeing this issue on a few of them. I'll investigate a little further and if I can't resolve it I'll open a ticket.

Thanks Rumen,
Joe
Tags
Editor
Asked by
Joe
Top achievements
Rank 2
Answers by
Rumen
Telerik team
Joe
Top achievements
Rank 2
Share this question
or