In a sub-site, I input HTML through the WI using the HTML Source edit button in the ribbon. This is what I enter:

 <a onclick="javascript:NewItem2(event, &quot;https://website/sites/site/_layouts/listform.aspx?PageType=8&amp;ListId={AEB087E7-DA21-4DA8-BB9A-9073382B3ED4}&amp;RootFolder=&quot;);javascript:return false;"href="/_layouts/listform.aspx?PageType=8&amp;ListId={AEB087E7-DA21-4DA8-BB9A-9073382B3ED4}&amp;RootFolder=" target="_self">

When I save the code, this is what it saves:

 <a href="/_layouts/listform.aspx?PageType=8&amp;ListId=%7bAEB087E7-DA21-4DA8-BB9A-9073382B3ED4%7d&amp;RootFolder=">

This code is followed by code for a linked image. Any idea why SP would do this? I have seen it update other code items to suit itself, but nothing to this extent.


Adding javascript to a Wiki page content is the same as adding it to a CEWP. The end result is that SharePoint will strip out the code and convert hyperlinks with JavaScript to useless tags, as you showed.

The only solution, which by the way is also a best practice, is to add a CEWP* to your page and link an external javascript file (that you'll have uploaded to a doc library in your site) in the content link box.

[*] As an alternative to the commonly proposed workaround of using a CEWP, you can make use of the HTML Form web part, which allows you to put javascript code within it, without incurring in your code being stripped out.

  • The CEWP works out. I have some formatting issues, but overall does what I need it to do. Had not thought of doing it this way ... guess that comes from being self taught in SP. Some best practices from formal training can sometimes can be a good thing ;-) – Pᴀᴜʟsᴛᴇʀ2 Feb 10 '14 at 19:12
  • 1
    Minor comment: the external file should actually not be a javascript file (.js), but rather a file with html content. To be more specific, it means that the <script> tag should be included in the file. – Christophe Jun 5 '14 at 18:20
  • @Christophe you are definitely right – MdMazzotti Jun 6 '14 at 9:06

Here are some guidelines from my blog:




They just confirm @MdMazzotti 's answer that a good practice is to link from the CEWP to an external file, and provide more explanations. The last article shows that a Page Viewer Web Part can also be considered for script injection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.