by Vesselin Vassilev
Posted On Apr 7, 2016
<!-- Facebook Pixel Code --> <script> !function(f, b, e, v, n, t, s) { if (f.fbq) return; n = f.fbq = function() { n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments) }; if (!f._fbq) f._fbq = n; n.push = n; n.loaded = !0; n.version = '2.0'; n.queue = []; t = b.createElement(e); t.async = !0; t.src = v; s = b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t, s); }(window,document,'script', '//connect.facebook.net/en_US/fbevents.js');fbq('init', '1480401912209426');fbq('track', 'PageView');</script> <noscript> <img height = '1'width = '1'style = "display:none"src = "https://www.facebook.com/tr?id=1480401912209426&ev=PageView&noscript=1" /> </noscript> <!-- End Facebook Pixel Code --><script type="application/ld+json">{ "@context" : "http://schema.org", "@type" : "Organization", "name" : "Your Organization Name", "url" : "http://www.your-site.com", "sameAs" : [ ]}</script>
public class CustomJavaScriptEmbedControlDesigner : JavaScriptEmbedControlDesigner{ protected override void InitializeControls(GenericContainer container) { base.InitializeControls(container); //the base method sets the HideAdvancedMode to True, but I want it visible so I can set //my custom IncludeScriptTag property to True or False this.PropertyEditor.HideAdvancedMode = false; }}[ControlDesigner(typeof(CustomJavaScriptEmbedControlDesigner))]public class CustomJavascriptWidget : JavaScriptEmbedControl{ public bool IncludeScriptTag { get; set; } private bool IsEdit { get { return this.IsDesignMode() && !this.IsPreviewMode(); } } protected override void OnPreRender(EventArgs e) { if (this.IncludeScriptTag) { base.OnPreRender(e); } else { if (this.Page != null && !this.IsEdit) { Literal literal = new Literal(); if (!string.IsNullOrEmpty(this.Url)) { string url = this.Url; if (url.StartsWith("~/")) { url = string.Concat(HostingEnvironment.ApplicationVirtualPath, url.Substring(1)); if (url.StartsWith("//")) { url = url.Substring(1); } } literal.Text = string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", url); } else if (!string.IsNullOrEmpty(this.CustomJavaScriptCode)) { literal.Text = this.CustomJavaScriptCode; } if (!string.IsNullOrEmpty(literal.Text)) { switch (this.ScriptEmbedPosition) { case ScriptEmbedPosition.Head: { this.Page.Header.Controls.Add(literal); return; } case ScriptEmbedPosition.InPlace: { this.Controls.Add(literal); return; } case ScriptEmbedPosition.BeforeBodyEndTag: { this.Page.ClientScript.RegisterStartupScript(typeof(CustomJavascriptWidget), this.ClientID, literal.Text, false); break; } default: { return; } } } } } }}