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

Calling a javascript from an onClick event

1 Answer 292 Views
WebParts for SharePoint
This is a migrated thread and some comments may be shown as answers.
Per Hoyer Stensnaes
Top achievements
Rank 1
Per Hoyer Stensnaes asked on 04 Sep 2009, 08:30 AM
Hi all
I have searched quite a bit to find an answer to this problem.
My scenario is WCM and i have followed the RadEditor For Moss included help file telling me to
In the config file:
<property name="AllowScripts">True</property>
In the layoutpage (articleleft.aspx in this scenario) opened in SharePoint Designer - i have changed the <PublishingWebcontrols:RichHTMLField ... with the <radE:RadHtmlfield.... and in that very control set the  AllowSpecialTags="True"

Now while the editor leaves the <script language="javascript">function xx(){alert("Hello world");}</script> uppon publishing it still strips my call for the xx function wich i try to call from a button <button onClick="xx">Click Me</button> - the "onClick=xx" has simply been stripped uppon publishing the pageinstance. This is also true for a <a href="javascript:xx()"... as well.

What do i do wrong? I think i have set all the props right according to the instructions provided by telerik?

I might ad that the problem dosnt occur in the webpart wich seems to accept any wich javascript code and functioncalls i want to use. BUT... i need to insert script in the WCM so...? please help.

1 Answer, 1 is accepted

Sort by
0
Per Hoyer Stensnaes
Top achievements
Rank 1
answered on 04 Sep 2009, 11:21 AM
I happened to stumble over the answer - but givin in the Full Feature. I have testet the solution and it works - its a little bit cumbersome, but... it worksl

---------------------------------------
WRONG APPROACH
---------------------------------------

<script type="text/javascript">
function xx()
{alert ("hello there");}
</script>
<div onClick="xx">Click me</div>

When you publish the page SharePoint (not telerik) will strip the onclick part leaving

<script type="text/javascript">
function xx()
{alert ("hello there");}
</script>
<div>Click me</div>

Funny that it leaves the appearently useless script section... when you cant call it from inline script anyway.

-------------------------------------------
THE RIGHT APPROACH
-------------------------------------------
<div id="myDiv">Click Me</div>
<script>document.getElementById("myDiv").onclick=function(){alert("hello there");}</script>

As said - a little cumbersome but it works...

Same goes for the different style attributes you might like to enter in a style=".... attribute, some of them will be stripped out too by sharepoint.

Wrong Approach
<table width="100%" style="background-image:url('myPic.jpg')">
<tr><td>.....

Right Approach
<table width="100%" id="myTable">
<tr><td>....
</table>
<script>document.getElementById("mytable").style.backgroundImage="url('myPic.jpg')";</script>

IT IS IMPORTANT that the script tag is placed AFTER the codeblock that needs it - dont ask me why, but it is...

Hope this helps someone out there.... :-)
Regards
Per Høyer
Tags
WebParts for SharePoint
Asked by
Per Hoyer Stensnaes
Top achievements
Rank 1
Answers by
Per Hoyer Stensnaes
Top achievements
Rank 1
Share this question
or