Allowing web editors to add javascript in EPiServer
The customer wanted to allow the web editors to add javascript in the Edit Mode that was then included in the <head> of the page. This is the easiest way I could figure out:
1. Add the following code to the header.ascx file:
<%=GetJavaScript()%>
2. Then put the following method in the code-behind file:
protected string GetJavaScript()
{
if(CurrentPage.Property.Exists(”Javascript”))
{
return CurrentPage.Property[”Javascript”].ToString();
}
else
{
return String.Empty;
}
}
3. Add an XHTML property called “Javascript” to any pages where you wish to have javascript.
4. Write/paste the javascript in the text box for this property. However, make sure that you paste it in the HTML edit mode, not ordinary text mode. Otherwise EPiServer will just reformat your javascript to html, which isn’t pretty.
02 December 2007