Is it possible to embed JavaScript Code inside the Content Editor of the Sitecore?

Hi  Co-developers on Sitecore Developer Network,

I have a technical question regarding embedding Javascript code snippet inside a Content Editor of Sitecore CMS Application. I'm having success in embedding HTML <iFrame> inside the editor. But I'm not that lucky enough in embedding the Javascript-code "<script></script>" inside the content editor of the Sitecore CMS.

  • Is it possible to embed JavaScript Code inside the Content Editor of the Sitecore?
  • As shown in the attached picture, I have embedded some JavaScript code, but I could not save the script code inside the content editor. (SDN only allows to save images of 64KB)
  • <p>Sitecore : Kailash  JavaScript  Custom Page </p>
    <div class="JScriptCustomPage" tabindex="1" id="jScriptCustomPageID">
           <a javascript:{alert("Hello Sitecore World : I'm Javascript");}>
    		Sitecore & JavaSciprt
           </a>
    </div>
    <script>
        /* SAME-ORIGIN POLICY UPDATE */
        
        // only if IE 11, add a meta tag that allows it to be compatible with IE 10 rules
        if(/Trident.*rv:/.test(navigator.userAgent)) { 
            var iemeta = document.createElement('meta');
            iemeta.httpEquiv = "x-ua-compatible";
            iemeta.content = "IE-10";
            document.getElementsByTagName('head')[0].appendChild(iemeta);
        }
    
        // then for all browsers, set the document.domain value to the root domain
        document.domain = location.hostname.split('.').reverse().splice(0,2).reverse().join('.'); 
    </script>

 

 

  • Again I'm not interested in keeping the JavaScript code inside the MVC Razor View (or) inside the C# Code-behind.

Can anyone of the Sitecore developers on this forum help?

Thanks & Regards,

Kailash

  • For security reasons sitecore removes script tags from rich text editor fields.

    However you can get around this with a multi-line text field, (and that probably makes more sense since the javascript won't be visible in the WYSIWYG editor anyway.)

    You might need to make a literal in the ascx file like this: <asp:Literal ID="litScript" runat="server"></asp:Literal> and use litScript.Text = Sitecore.Context.Item["Body Script"]; in the code behind though.

  • In addition to the previous post, it is also possible to disable that feature.
    Just go to the Sitecore.config to the following entry
    <!-- HTML EDITOR REMOVE SCRIPTS
    If true, the rich text editor removes script tags and inline scripts from RTE field values before saving. Setting the value to true reduces the potential for cross-site scripting and other script-related issues.
    Default value: true
    -->
    <setting name="HtmlEditor.RemoveScripts" value="true" />

    and set it to false.
    But in general I agree, that this is not recommended, but if other apporaches wont work, this would also be an option.
  • Use single line text or multiline text and then use @HTML.Raw() to output js tags
  • If you are using SXA, you can use Plain HTML component to do this.