Friday, 7 December 2012

Javascript or Css disappears in Content Editor Webpart - CEWP


Recenlty I faced a problem that I was writing some html along with javascript tag with some code in the CONTENT EDITOR WEBPART (CEWP). As soon as I save the page, all the tags of javascript disppeared or removed from CEWP. I googled, but did not find any solution.


What I did, encapsulated the script and html within "CDATA" section, actually sharepoint 2010, it was removing all such script tags from the CEWP becasuse of secrutiy purpose, it removes all the script which could threaten or inject. Thus CDATA solved my problem.

Updated (Feb 18 2013) :
On my friend's request FAZ, am including an example to explain it littile bit more on this topic.

Sometimes you edit a page in sharepoint designer, and add javascript snippet in the page, on saving the page it prompt you a message that "unsafe code will removed...", and when the page is saved you will notice that the javascript snippet you just added is removed.
For Example:
1. Edit a page and add some javascript code snippet in the Content Editor Webpart and save.
 (Content Editor webpart is inserted using "Insert" tab in the SharePoint Designer, and then "Webpart" menu, and then select "Content Editor")
2. When you will save , it will prompt a message just shown in the figure below:

3. When you will click "Yes", you will notice that the javascript snipped you just added have been removed from the page.
So for that we have to keep our javsacirpt / CSS snippet in CDATA wrapping as shown below:

<script type="text/javascript"><![CDATA[
function Test()
]] </script>

Then it will not remove your snipped from the page.

Hope this help!!


  1. Nice article. Can you please explain it little bit more with any example?

    1. Faz, I have updated my post, and have added an example to explain it little bit more. Thanx!!

  2. I am attempting to embed an MP4 video within a CEWP. It works while I'm in edit mode, but as soon as I save, the embed code disappears. Will your solution above help with this as well? I've tried it, but I think I'm doing something wrong. Thanks!

    Code that is disappearing:

    1. Hi Heather, Can you please tell me what code is being disappeared and how you are using my techinque to avoid this? In your comments, you have not mentioned any code.